htc g17测试。
一、 BluetoothAdapter 代表本地蓝牙设备。
1.得到本地蓝牙设备
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
2.打开蓝牙
(1)、bluetooth.enable()(需要android.permission.BLUETOOTH_ADMIN权限)。bluetooth.disable()关闭。
(2) 会弹一个对话框,确定就打开。
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
2.1、isEnabled() 是否打开蓝牙。
3、蓝牙名字得到名字:bluetooth.getName();
设置名字:bluetooth.setName(String name);
4、蓝牙mac地址
bluetooth.getAddress();
5、使设备可见
(1)、bluetooth.startDiscovery(),扫描周围蓝牙设备,持续大约12秒。
(2)、弹对话框,确定就会使设备可被搜索,如果没开蓝牙,会自动打开
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivity(intent);
如果想设置蓝牙的可见时间,在startActivity之前加上
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
蓝牙可见时间不能超过300秒,否则会被300秒覆盖。
5.1 isDiscovering() 是否处于可被搜索状态。测试永远都是false。
6、蓝牙状态
getState(),返回int值,值代表各自状态。
可能的值有: STATE_OFF = 10,STATE_TURNING_ON =11, STATE_ON =12,STATE_TURNING_OFF =13.
分别代表已关闭、打开中、已打开、关闭中。
7、得到本地存储的远端设备信息
(1)、根据mac地址得到,mac必须大写,不一定本地存储了。
BluetoothDevice bd = bluetooth.getRemoteDevice (String address)
(2)、得到本地存储的所有远端蓝牙信息的集合,如果蓝牙没开,set大小为0。
Set<BluetoothDevice>set = bluetooth.getBondedDevices();
对蓝牙的各种操作有各种广播返回,接收广播的action获取信息。比如扫描到蓝牙设备后会收到action为
BluetoothDevice.ACTION_FOUND的信息。
待续.....