BlE目前已经应用很广泛了,网上能查到很多相关的连接,发送信息的资料,这里记录一些基本用法和几个查了很久找到的方法,还有一些常用Utils.
1,获得蓝牙适配器:适配器模式有很多优点,当然蓝牙的基本操作都是通过蓝牙适配器进行的.
BluetoothManager mBluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
2,打开蓝牙的操作
if (bluetoothAdapter.isEnabled()) return;
bluetoothAdapter.enable();//isEnabled()是判断蓝牙是否已经打开
3,扫描周围的蓝牙设备
bluetoothAdapter.startDiscovery();//扫描蓝牙
4,停止扫描
bluetoothAdapter.cancelDiscovery();
5,连接蓝牙设备,目前我们ble版本的官方api和之前非ble的有很大不同,也多出了很多类事业周边,特征等等的一些新的概念.
a)结构图(网上找的,还有水印…..):
b)简单的说就是:
在BLE协议中,蓝牙设备根据功能的不同有两个角色周边(Periphery)和中央(Central),周边是数据提供者,中央是数据使用/处理者;
每一个周边BluetoothGattServer,包含多个服务Service,每一个Service包含多个特征Characteristic。
我们的目的就是通过中央设备根据提供的周边设备的特征码(UUID)连接这个周边设备的Characteristic然后就可以进行一些读写的操作.
b1)
mDevice = mBluetoothAdapter.getRemoteDevice(address);//address就是你链接的设备的地址,通过上面的连接操作获得的device对象可以获得
mBluetoothGatt = mDevice.connectGatt(this, false,
mBluetoothGattCallback);
mBluetoothGattCallback是一个回调,定义个ble各个状态的回调方法,可以根据callback里的方法进行相应的操作,例如设备连接和断开,读和写的回调.
BluetoothGattCallback mBluetoothGattCallback = new BluetoothGattCallback() {
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
//状态改变
if (status == BluetoothGatt.GATT_SUCCESS) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
showMessage("BLE Connect !!!");
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
showMessage("BLE DisConnect !!!");
}
}
};
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
//发现服务
if ((status == BluetoothGatt.GATT_SUCCESS)
&& (mBluetoothGatt.getService(SERVICE_UUID) != null)) {
showMessage("Discover services Successful");
}
};
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
//写操作
showMessage("Characteristic write successful!");
}
};
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
showMessage("RSSI:" + rssi );//蓝牙信号强度
}
};
};
b2)根据服务和特征的UUID写消息
service = mBluetoothGatt.getService(SERVICE_UUID);//根据服务的UUID获取服务
characteristic = service.getCharacteristic(CHARACTERISTIC_UUID);//根据Characteristic的UUID获取Characteristic
characteristic.setValue(writeString);//writeString是要write到周边的内容
mBluetoothGatt.writeCharacteristic(characteristic);
}
5 ,其他的我就不写了,这里介绍几个有用的方法
查看已经配对和已连接的设备
注意这里区分: 已经配对设备是连接过的蓝牙设备,已连接设备是当前正连接状态可以传输数据的设备.
首先:
BluetoothAdapter mBTAdapter = BluetoothAdapter.getDefaultAdapter();
mBTAdapter.getProfileProxy(context, mProfileListener,
BluetoothProfile.A2DP);
查找已经配对和已连接的设备就是在上面参数mProfileListener里进行的:
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (proxy == null) {// so, bluetooth binder maybe null
return;
}
Set<BluetoothDevice> pairedDevices = mBTAdapter.getBondedDevices();//已配对
List<BluetoothDevice> connectedDevices = proxy.getConnectedDevices();//已连接
//然后就可以使用这两个集合处理相应逻辑了
//可以节省再次扫描查找的时间
...........
}
查找已配对设备:
private void getBondedDevices(ArrayList<BluetoothDevice> list) {
Set<BluetoothDevice> devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
if (devices.size() > 0) {
for (Iterator<BluetoothDevice> it = devices.iterator(); it.hasNext(); ) {
BluetoothDevice device = (BluetoothDevice) it.next();
......
}
}
}
6,有一点提醒一下在你决定执行
mBluetoothGatt = mDevice.connectGatt(this, false,
mBluetoothGattCallback);
去连接蓝牙之前,别忘了先
mBTAdapter.cancelDiscovery();//停止扫描,释放资源
当然在获取服务和特征的时候也要进行类似操作.(因为服务和特征也有类似的mBluetoothGatt.discoverServices()方法,这里就不写了)
7,蓝牙操作中我们经常会收集的同时发送数据,
Collections.synchronizedList(new xxxxx);
可以有效的解决list的并发修改异常
java.util.ConcurrentModificationException
总结:以前版本的api完全也可以实现ble的所有操作,之前公司进行的BLE项目就是应用的老版本的api,可以完美运行,但是新版本总有新版本的好处吧.