完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
路过的朋友,本人最近学习蓝牙过程中,发现收不到广播,请有经验的兄弟们给点儿建议:
package com.example.first; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manifest; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Adapter; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Set; public class MainActivity extends AppCompatActivity { private String[] string1= { "1","2","3","4"}; private String[] string2= { "A","B","C","D"}; private Button bt1,bt2; private BluetoothAdapter BA; private Set private ListView ls1,ls2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter MainActivity.this, android.R.layout.simple_list_item_1,string1 ); ArrayAdapter MainActivity.this, android.R.layout.simple_list_item_1,string2 ); ListView ls1 = (ListView) findViewById(R.id.list1); ListView ls2 = (ListView) findViewById(R.id.list2); ls1.setAdapter(adapter1); ls2.setAdapter(adapter2); mReceiver mReceiver = new mReceiver(); IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy } public class mReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { TextView text2 = (TextView) findViewById(R.id.text2); text2.setText("AAA"); String action = intent.getAction(); // When discovery finds a device Toast.makeText(getApplicationContext(), "find object" , Toast.LENGTH_LONG).show(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object from the Intent text2.setText("CCC"); ArrayList list = new ArrayList(); BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Add the name and address to an array adapter to show in a ListView list.add(device.getName() + "\n" + device.getAddress()); ArrayAdapter MainActivity.this, android.R.layout.simple_list_item_1, list ); ls2.setAdapter(adapter3); } // 收索接收函数需要注册: // Register the BroadcastReceiver } }; public void checkBlePermission() { TextView text1 = (TextView) findViewById(R.id.text1); if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1); text1.setText("1A"); } else { Toast.makeText(getApplicationContext(),"已申请权限" ,Toast.LENGTH_LONG).show(); text1.setText("2A"); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); TextView text2 = (TextView) findViewById(R.id.text2); switch (requestCode) { case 1: { // 如果请求被取消,则结果数组为空。 if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(getApplicationContext(),"同意申请" ,Toast.LENGTH_LONG).show(); text2.setText("3A"); } else { Toast.makeText(getApplicationContext(),"拒绝申请" ,Toast.LENGTH_LONG).show(); text2.setText("4A"); } return; } default : text2.setText("8A"); } } public void bt1_click(View v){ TextView text1 = (TextView) findViewById(R.id.text1); Spinner Sp1 = (Spinner) findViewById(R.id.spinner1) ; String sl_text1 = String.valueOf(Sp1.getSelectedItem()) ; text1.setText(sl_text1); BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter(); if(!BA.isEnabled()){ Intent turnon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(turnon,0); Toast.makeText(getApplicationContext(),"Turned on" ,Toast.LENGTH_LONG).show(); } else{ Toast.makeText(getApplicationContext(),"already on" ,Toast.LENGTH_LONG).show(); } checkBlePermission(); BA.startDiscovery(); }; public void bt2_click(View v){ TextView text2 = (TextView) findViewById(R.id.text2); Spinner Sp2 = (Spinner) findViewById(R.id.spinner2) ; String sl_text2 = String.valueOf(Sp2.getSelectedItem()) ; text2.setText(sl_text2); }; } |
|
相关推荐
1 条评论
1个回答
|
|
问题已解决,一方面是程序流程问题,最终的顺序:使能->权限检查->扫描得到btDev->绑定->显示可配对设备->......,二是没有开GPS(没开GPS在华为S7上能用,在三星A51就不能了),
已经可以通信,但是坑还没填完。。 |
|
|
|
只有小组成员才能发言,加入小组>>
推荐一本书《android应用程序开发与典型案例》完整版PDF下载
43761浏览 120评论小黑屋| 手机版| Archiver| 电子发烧友 ( 粤ICP备14022951号 )
GMT+8, 2022-11-1 20:37 , Processed in 0.468494 second(s), Total 124, Slave 106 queries .
Powered by 电子发烧友网
© 2015 www.ws-dc.com