1.进行操作前要把蓝牙权限打开
在AndroidManifest中添加权限命令
<uses-permission android:name="android.permission.BLUETOOTH" />2.继承OnClickListener
当按下按钮时
public void onClick(View v) { BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); //得到BluetoothAdapter对象 if(adapter != null) { //判断对象是否为空 System.out.println("本机拥有蓝牙设备"); if(!adapter.isEnabled()) { Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(intent); } Set<BluetoothDevice> devices = adapter.getBondedDevices(); if(devices.size() >0) { for(Iterator iterator = devices.iterator(); iterator.hasNext();) { BluetoothDevice bluetoothDevices = (BluetoothDevice) iterator.next(); System.out.println(bluetoothDevices.getAddress()); } } } else { System.out.println("没有蓝牙设备"); } }得到BluetoothAdapter对象(得到适配器)
如果适配器adapter对象为空,在控制台上打印没有蓝牙设备。
如果不为空,打印本机拥有蓝牙设备。---------
Intent : 显式Intent 从当前活动跳转到蓝牙响应活动 最后再跳转回来
ACTION_REQUEST_ENABLE 请求用户选择是否打开蓝牙
获得BluetoothDevice (远程蓝牙设备)
的地址MVC消息
