三、搜索设备
//定义对象
private BluetoothLeScanner bluetoothLeScanner;
private List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();//存放扫描结果
//startScan()回调函数
private ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult results) {
super.onScanResult(callbackType, results);
BluetoothDevice device = results.getDevice();
if (!devices.contains(device)) { //判断是否已经添加
devices.add(device);//也可以添加devices.getName()到列表,这里省略 }
// callbackType:回调类型
// result:扫描的结果,不包括传统蓝牙 }
};
//在onCreat()中添加
bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
bluetoothLeScanner.startScan(mScanCallback);//android5.0把扫描方法单独弄成一个对象了(alt+enter添加),扫描结果储存在devices数组中。最好在startScan()前调用stopScan()。
- 扫描是很占用资源的,这里添加定时器10s停止搜索
private Runnable runnable = new Runnable() {
@Override
public void run() {
bluetoothLeScanner.stopScan(scanCallback);
}
};
private Handler handler = new Handler();//import android.os.Handler;
//然后在onclick()中调用即可
hander.postDelayed(runnable, 10000);
bluetoothDevice对象中储存着设备的所有信息 例:
- .getName() 获取名称
- getUuids() 获取UUID
- getAddress() 获取MAC地址
等等
四、建立连接
private BluetoothDevice bluetoothDevice;
private BluetoothGatt bluetoothGatt;
//bluetoothDevice是dervices中选中的一项 bluetoothDevice=dervices.get(i);
//选择bluetoothDevice后配置回调函数
bluetoothGatt=bluetoothDevice.connectGatt(MainActivity.this, false, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState){
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {//状态变为 已连接
Log.e(TAG, "成功建立连接");
}
gatt.discoverServices();//连接成功,开始搜索服务,一定要调用此方法,否则获取不到服务
if (newState == BluetoothGatt.STATE_DISCONNECTED) { //状态变为 未连接
Toast.makeText(MainActivity.this, "连接断开", Toast.LENGTH_LONG).show();
}
return;
}
public void onServicesDiscovered(BluetoothGatt gatt, final int status) {
//用此函数接收数据
super.onServicesDiscovered(gatt, status);
return;
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
//发现服务后的响应函数
}
});
五、服务、特征
private BluetoothGattService bluetoothGattService;
private BluetoothGattCharacteristic bluetoothGattCharacteristic;
//在onServicesDiscovered()中加入以下代码
onServicesDiscovered()
{
super.onServicesDiscovered(gatt, status);
String service_UUID = "0000ffe0-0000-1000-8000-00805f9b34fb";//已知服务
String characteristic_UUID = "0000ffe1-0000-1000-8000-00805f9b34fb";//已知特征
bluetoothGattService = bluetoothGatt.getService(UUID.fromString(service_UUID));//通过UUID找到服务
bluetoothGattCharacteristic = bluetoothGattService.getCharacteristic(UUID.fromString(characteristic_UUID));//找到服务后在通过UUID找到特征
if (bluetoothGattCharacteristic != null) {
gatt.setCharacteristicNotification(bluetoothGattCharacteristic, true);//启用onCharacteristicChanged(),用于接收数据
//Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this, "发现服务失败", Toast.LENGTH_LONG).show();
return;
}
}
为节约系统资源,官方将128位中的其他位固定,只保留16位BASE UUID 是 0000xxxx-0000-1000-8000-00805F9B34FB,其中xxxx为16位的UUID
JDY的service 为FFE0,Characteristic为FFE1
如果不知道UUID,可以用
**List services = bluetoothGatt.getServices();//搜索所有服务
services.get(i).getUuid();//通过变量i获取所有UUID****ListCharacteristics=bluetoothGattService.getCharacteristics();//搜索所有Characteristic
Characteristics.get(i).getUuid();//通过变量i获取所有UUID****//当然如果需要读取descriptor 也可以用相同的方法获取UUID
List descriptors = bluetoothGattCharacteristic.getDescriptors();
descriptors.get(i).getUuid();**
根据一些用的UUID确定每个UUID的功能,或者逐一尝试,如果只需要收发数据可以不需要descriptor的