Android蓝牙开发
16lz
2022-06-13
蓝牙设备的权限
首先需要在AndroidManifest.xml中加入这两个蓝牙权限
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
打开蓝牙
创建BluetoothAdapter对象bluetoothAdapter
privateBluetoothAdapterbluetoothAdapter=BluetoothAdapter.getDefaultAdapter;
通过此方法可以直接打开蓝牙
publicvoidonClickOpenBluetooth(Viewview){bluetoothAdapter.enable();}
或使用此方法,会有对话框提示
publicvoidonClickOpenBluetoothOnDialog(Viewview){Intentintent=newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(intent,1);}
关闭蓝牙
此方法可以直接关闭蓝牙
publicvoidonClickCloseBluetooth(Viewview){bluetoothAdapter.disable();}
搜索蓝牙设备
布局文件main.xml
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:onClick="onClick_Search"android:text="开始扫描"/><ListViewandroid:id="@+id/lv"android:layout_width="fill_parent"android:layout_height="wrap_content"/>
MainActivity.java
//定义好这些控件privateListViewlvDevices;privateList<String>bluetoothDevices=newArrayList<String>();privateArrayAdapter<String>arrayAdapter;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);setContentView(R.layout.main);//获取系统已经绑定的蓝牙设备,方法插入到onCreate()方法中Set<BluetoothDevice>pairedDevices=bluetoothAdapter.getBondedDevices();if(pairedDevices.size()>0){for(BluetoothDevicedevice:pairedDevices){bluetoothDevices.add(device.getName()+":"+device.getAddress()+"\n");}}arrayAdapter=newArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,bluetoothDevices);lvDevices.setAdapter(arrayAdapter);//注册搜索时的蓝牙设备的receiverIntentFilterfilter=newIntentFilter(BluetoothDevice.ACTION_FOUND);this.registerReceiver(receiver,filter);//注册搜索完成时的蓝牙设备的receiverfilter=newIntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);this.registerReceiver(receiver,filter);}/***搜索蓝牙设备**@paramview*/publicvoidonClick_Search(Viewview){setProgressBarIndeterminateVisibility(true);setTitle("搜索设备");bluetoothDevices.clear();if(bluetoothAdapter.isDiscovering()){bluetoothAdapter.cancelDiscovery();}bluetoothAdapter.startDiscovery();}/***写一个广播*/privatefinalBroadcastReceiverreceiver=newBroadcastReceiver(){@OverridepublicvoidonReceive(Contextcontext,Intentintent){Stringaction=intent.getAction();//如果检测到现在是扫描状态if(BluetoothDevice.ACTION_FOUND.equals(action)){//搜索设备BluetoothDevicedevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);//如果搜索到的设备不是已经绑定的设备if(device.getBondState()!=BluetoothDevice.BOND_BONDED){//将搜索到的设备的名称和地址显示出来bluetoothDevices.add(device.getName()+":"+device.getAddress());//通知适配器设置更改adapter.notifyDataSetChanged();}}//搜索完成elseif(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){setTitle("扫描完成");setProgressBarIndeterminateVisibility(false);}}};
更多相关文章
- Android生命周期的学习
- Android(安卓)Service Bind启动调用service方法
- Android(安卓)退出程序方式——再按一次退出
- Android(安卓)inject input events 注入Touch 点(x, y) 触摸输入
- Android获取屏幕宽高的方法
- android image cache 图片缓存 异步 下载
- 【移动安全】Android(安卓)App Smail代码动态跟踪调试方法
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用