暂无
repositories {
jcenter() //使用jcenter
mavenCentral(); //使用maven
//maven远程仓库地址
maven {
url 'https://dl.bintray.com/botasky/maven'
}
}
dependencies {
compile 'com.sirius.botasky:cyberble:1.0.1'
}
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);//直接传入已连接上的蓝牙设备地址
}
});
private DeviceConnectStateCallback mDeviceCallBack = new DeviceConnectStateCallback() {
@Override
public void onDeviceConnected(final String address) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//连接成功后去发现设备的服务。停止搜索
mBleAdmin.discoverDeviceServices(address);
mBleAdmin.stopScan();
discover.setVisibility(View.GONE);
connect.setVisibility(View.VISIBLE);
}
});
}
@Override
public void onDeviceDisconnected() {
runOnUiThread(new Runnable() {
@Override
public void run() {
//设备断开连接的回调
discover.setVisibility(View.VISIBLE);
connect.setVisibility(View.GONE);
}
});
}
};
private DeviceOperationCallback mDeviceOperationCallback = new DeviceOperationCallback() {
@Override
public void onDeviceServiceDiscover(String deviceAddress, List<BluetoothGattService> services) {
//发现服务回调
displayGattServices(services);
}
@Override
public void onDeviceCharacteristicRead(String deviceAddress, BluetoothGattCharacteristic characteristic) {
//读操作回调
displayData(characteristic);
}
@Override
public void onDeviceCharacteristicWrite(String deviceAddress) {
//写操作回调
}
@Override
public void onDeviceCharacteristicNotify(String deviceAddress, BluetoothGattCharacteristic characteristic) {
//Notify得到的回调
displayData(characteristic);
}
};