BleAdmin.java ,蓝牙设备控制类,用来做蓝牙设备管理,扫描等
BleOperator.java 对应一个蓝牙设备,管理设备的READ,INDICATE,NOTIFY,WRITE,CONNECT,DISCONNECT等操作
BleDeviceService.java 用来描述蓝牙READ,INDICATE,NOTIFY,WRITE这四个功能的类
callback 下面的类主要是控制一些回调
//mDeviceCallBack是蓝牙连接状态回调
//mDegviceOperationCallback是蓝牙操作的回调,WRITE,NOTIFY等操作的回调
mBleAdmin = new BleAdmin(this, mDeviceCallBack, mDeviceOperationCallback);
//打开蓝牙
mBleAdmin.openBle();
//开始搜索,设置搜索回调
mBleAdmin.startScanAllDevice(new ScanCallback() {
@Override
public void onDeviceFound(final List<BluetoothDevice> devices) {
Log.e("Found", " " + devices.size());
runOnUiThread(new Runnable() {
@Override
public void run() {
//显示设备要在主线程
mRecyclerAdapter.setDevices(devices);
}
});
}
});
viewHolder.content.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentDeviceAddress = device.getAddress();
//mBleAdmin.connectDevice(device.getAddress());用地址直接连接
mBleAdmin.connectDevice(device);//用搜索到的设备直接连接, 推荐
}
});
//开启READ操作
mBleAdmin.processDeviceService(new BleDeviceService(
mCurrentDeviceAddress, //已连接的要开启读操作蓝牙的地址
characteristic.getUuid(), //读操作的特征值
BleDeviceService.OperateType.Read));//设置OperationType为READ
//开启Notify操作
mBleAdmin.processDeviceService(new BleDeviceService(
mCurrentDeviceAddress, //开启Notify的已连接上的蓝牙的地址
characteristic.getUuid(), //Notify操作的特征值
BleDeviceService.OperateType.Notify));//设置OperationType为NOTIFY
mWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBleAdmin.processDeviceService(new BleDeviceService(
mCurrentDeviceAddress,//开启Write的已连接上的蓝牙的地址
UUID.fromString(SampleGattAttributes.INSOLE_WRITE),//Write的UUID特征值
BleDeviceService.OperateType.Write,//OperationType
mEditData.getText().toString().getBytes()));//要写入的byte[]值
}
});
mDisconnecte.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//mBleAdmin.disconnectDevice(mCurrentDevce);//或者直接传入设备
mBleAdmin.disconnectDevice(mCurrentDeviceAddress);//直接传入已连接上的蓝牙设备地址
}
});