1.android 手机的低功耗蓝牙,又称BLE ;BLE在andriod 4.3 以上才支持,又称蓝牙4.0,区别于经典蓝牙,BLE 低功耗,手机是否支持低功耗蓝牙,主要取决于手机硬件,所以使用前,需要先进行判断,是否支持低功耗蓝牙
2.蓝牙的使用,
1.判断mobile 是否有低功耗蓝牙,返回值boolean
mainActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)
2.获取bluetooth的adapter,adapter为null,证明设备无蓝牙
mDefaultAdapter = BluetoothAdapter.getDefaultAdapter();
3.设备是否已经开启蓝牙
mDefaultAdapter.isEnabled()
4.打开蓝牙,开启蓝牙有两种方式,第一种,通过intent开启,会给用户以提示。
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
mainActivity.startActivityForResult(intent, OPEN_BLUETOOTH_REQUEST_CODE);
//在activity中获取开启蓝牙的反馈
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
switch (requestCode){
case OpenBluetoothPresenter.OPEN_BLUETOOTH_REQUEST_CODE:
if(requestCode==RESULT_OK){ //result_ok 是activity中的常量,为-1,说明操作成功了
mOpenBluetoothPresenter.scanDevice();
}else{
ToastUtils.showMessage(this,getString(R.string.connect_device_need_open_bluetooth));
}
break;
}
}
第二种方式,这种方式会直接开启蓝牙,但是不能监听到用户是否真的开启了蓝牙;但是可以通过广播监听蓝牙状态的变化,建议非系统应用,采用第一种开启方式
mDefaultAdapter.enable();
5.扫描设备,因为我所使用的低功耗设备,而且只需要扫描低功耗设备,我采用的是startLeScan(),改方法过时了,建议采用bluetoothAdapterScanner.startScan,但是这个函数,要求sdk>23,而我需要支持的18,所以仍采用startLeScan
mDefaultAdapter.startLeScan(this);
6.扫描的结果,回调OnLEScan里面,改方法是实现接口:BluetoothAdapter.LeScanCallback
@Override
public void onLeScan (BluetoothDevice device, int rssi, byte[] scanRecord) {
boolean isDeviceFinded = false;
String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0 /*&& deviceName.startsWith("M")*/) {
if(devices==null){
devices = new ArrayList<>();
devices.add(device);
}else{
for (BluetoothDevice bluetoothDevice : devices) {
if(bluetoothDevice.getName().equals(deviceName)){
isDeviceFinded = true;
break;
}
}
if(!isDeviceFinded){
devices.add(device);
}
openBluetoothView.notifyDeviceList(devices);
}
}
}
7,停止搜索
mDefaultAdapter.stopLeScan(OpenBluetoothPresenter.this);
8.连接设备,连接设备是通过bluetoothdevice的address进行连接,但是连接过程中,还是要进行一系列的判断,进行优化
if (mDefaultAdapter == null || address == null) {
ToastUtils.showMessage(mainActivity,mainActivity.getString(R.string.no_connect_device));
return;
}
BluetoothDevice remoteDevice = mDefaultAdapter.getRemoteDevice(address);
if (remoteDevice == null) {
ToastUtils.showMessage(mainActivity,mainActivity.getString(R.string.connect_fail_no_device));
return;
}
remoteDevice.connectGatt(mainActivity,false,this);
9.连接的结果,会返回到BluetoothGattCallback里面,BluetoothGAttCallback是一个抽象类,里面的函数意义,:
/**
* 回调指示,
*
* @param gatt gatt客户端
* @param status 连接或者断开操作的状态,返回是BlutoothGatt 操作是否成功
* @param newState 新连接的状态,断开,或者连接bluetprofile state_connect,state_disconnect
*/
@Override
public void onConnectionStateChange (BluetoothGatt gatt, int status, int newState) {
Log.e(TAG, "onConnectionStateChange: status" + status + " status" + BluetoothGatt.GATT_SUCCESS + newState + ">>" + BluetoothProfile.STATE_CONNECTED);
}
/**
* 连接设备的services,characteristrics,desriptors 更新的时候调用
*
* @param gatt gatt客户端
* @param status 连接设备已经被探索成功,是BluetoothGatt.Gatt_successfuly
*/
@Override
public void onServicesDiscovered (BluetoothGatt gatt, int status) {
Log.e(TAG, "onServicesDiscovered: " + status + " sss" + BluetoothGatt.GATT_SUCCESS);
}
/**
* 读到特征值得结果
*
* @param gatt 客户端
* @param characteristic 特征值
* @param status 读的操作完成后,status 是BluetoothGatt.Gatt_Success
*/
@Override
public void onCharacteristicRead (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.e(TAG, "onCharacteristicRead: " + status);
}
/**
* 设备的特征值变化的时候,该函数被触发
*
* @param gatt 客户端
* @param characteristic 变换后的特征值
*/
@Override
public void onCharacteristicChanged (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.e(TAG, "onCharacteristicChanged: 调用了");
}
/**
* 在调用mBluetoothGatt.writeCharacteristic()函数之后,接收该函数操作的结果
* @param gatt 客户端
* @param characteristic 特征值,这个特征值是从远程设备返回的之前写入的值,应用程序应该进行对比,如果不正确,进行操作
* @param status 如果写入成功,这个返回的BluetoothGatt 的Gatt_success
*/
@Override
public void onCharacteristicWrite (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
}
10.remoteDevice connectGatt()会返回一个GAtt对象,gatt connect方法会重新连接一个已经连接过的连接
BluetoothGatt mBluetoothGatt = remoteDevice.connectGatt(mainActivity, false, this);
//如果与远程设备的连接断开,调用该函数会重新进行连接,如果设备不在连接范围内,
//会等设备出现在连接范围内后,调用一次,进行连接
mBluetoothGatt.connect();
11.向蓝牙发送指令,通过的是串口服务
//当write操作完成后,会回调BluetoothGattCallback的onCharacteristicWrite函数,回报操作的结果,
//返回true,写入成功
boolean b = mBluetoothGatt.writeCharacteristic(rxServiceChar);
12.开启Characteritics特征
//开启或者关闭指定的特征值,Boolean 参数即为开启或关闭操作,该函数返回值,是操作是否成功
//返回的结果会在BluetoothGattCallback里面的onCharacteriticsChang调用
mBluetoothGatt.setCharacteristicNotification(characteristic,true);
13.获取远程设备的所有服务
mBluetoothGatt.getServices();
14.断开连接
public void disconnect(){
if(mDefaultAdapter==null||mBluetoothGatt==null){
Log.d(TAG, "disconnect: mDefaultAdapter或者 mBluetoothGatt为null,无法关闭");
return;
}
mBluetoothGatt.disconnect();
}
15.清除对象
public void close(){
if(mBluetoothGatt == null){
Log.d(TAG, "close: mBluetoothGatt 位null,已经close");
return;
}
mOldBluetoothAddress = null;
mBluetoothGatt.close();
mBluetoothGatt = null;
}