Android 蓝牙BLE开发详解

蓝牙简介

低功耗蓝牙
传统蓝牙

蓝牙发展至今经历了8个版本的更新。1.1、1.2、2.0、2.1、3.0、4.0、4.1、4.2。那么在1.x~3.0之间的我们称之为传统蓝牙,4.x开始的蓝牙我们称之为低功耗蓝牙也就是蓝牙ble,当然4.x版本的蓝牙也是向下兼容的。
Android手机必须系统版本4.3及以上才支持BLE API。通过GATT协议进行BLE设备之间的通信。
优点:传输速度更快,覆盖范围更广,安全性更高,延迟更短,耗电极低等等优点

BLE分为三部分:Service,Characteristic,Descriptor。这三部分都用UUID作为唯一标识符

一个BLE终端可以包含多个Service(服务)
一个Service可以包含多个Characteristic(特征)
一个Characteristic包含一个value和多个Descriptor(描述符),一个Descriptor包含一个Value。

Characteristic是比较重要的,是手机与BLE终端交换数据的关键,读取设置数据等操作都是操作Characteristic的相关属性

接下来讲解开发步骤

1、添加权限

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

2:判断设备是否支持蓝牙ble

// 检查当前手机是否支持ble 蓝牙,如果不支持退出程序
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
        finish();
}

3:获取蓝牙适配器BluetoothAdapter

通过以下两种方式获取

BluetoothManager bluetoothManager =(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
或
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();     
//如果mBluetoothAdapter == null,说明设备不支持蓝牙

4:弹出是否启用蓝牙的对话框

if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	switch (requestCode) {
	case REQUEST_ENABLE:
		if (resultCode == Activity.REQUEST_ENABLE_BT) {
			Toast.makeText(this, "蓝牙已启用", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(this, "蓝牙未启用", Toast.LENGTH_SHORT).show();
		}
		break;
	}
}

5:搜索设备

 private void scanLeDevice(final boolean enable) {
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                }
            }, SCAN_PERIOD); //10秒后停止搜索
            mScanning = true;
            mBluetoothAdapter.startLeScan(mLeScanCallback); //开始搜索
        } else {
            mScanning = false;
            mBluetoothAdapter.stopLeScan(mLeScanCallback);//停止搜索
        }
    }

6:搜索到设备会回调LeScanCallback接口

private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //在这里可以把搜索到的设备保存起来		
                    //device.getName();获取蓝牙设备名字
                    //device.getAddress();获取蓝牙设备mac地址
                }
            });
        }
    };

7:选择一个设备进行连接。连接后会返回一个BluetoothGatt 类型的对象,这里定义为mBluetoothGatt。该对象比较重要,后面发现服务读写设备等操作都是通过该对象。

代码里建了一个service,里面封装了连接,读写设备等操作。连接是通过获取到的mac地址去进行连接操作就可以了。
BluetoothGatt()下的方法:

connect() :连接远程设备。
discoverServices() : 搜索连接设备所支持的service。
disconnect():断开与远程设备的GATT连接。
close():关闭GATT Client端。
readCharacteristic(characteristic) :读取指定的characteristic。
setCharacteristicNotification(characteristic, enabled) :指定characteristic值变化时,发出通知。
getServices() :获取远程设备所支持的services。

public boolean connect(final String address) {
		if (mBluetoothAdapter == null || address == null) {
			Log.w(TAG,"BluetoothAdapter not initialized or unspecified address.");
			return false;
		}
		// Previously connected device. Try to reconnect. (先前连接的设备。 尝试重新连接)
		if (mBluetoothDeviceAddress != null&& address.equals(mBluetoothDeviceAddress)&& mBluetoothGatt != null) {
			Log.d(TAG,"Trying to use an existing mBluetoothGatt for connection.");
			if (mBluetoothGatt.connect()) {
				mConnectionState = STATE_CONNECTING;
				return true;
			} else {
				return false;
			}
		}
		final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
		if (device == null) {
			Log.w(TAG, "Device not found.  Unable to connect.");
			return false;
		}
		// We want to directly connect to the device, so we are setting the
		// autoConnect
		// parameter to false.
		mBluetoothGatt = device.connectGatt(this, false, mGattCallback); //该函数才是真正的去进行连接
		Log.d(TAG, "Trying to create a new connection.");
		mBluetoothDeviceAddress = address;
		mConnectionState = STATE_CONNECTING;
		return true;
	}

8:连接后会回调BluetoothGattCallback接口,包括读取设备,往设备里写数据及设备发出通知等都会回调该接口。其中比较重要的是BluetoothGatt。

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
		@Override  //当连接上设备或者失去连接时会回调该函数
		public void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) {
			if (newState == BluetoothProfile.STATE_CONNECTED) { //连接成功
                        mBluetoothGatt.discoverServices(); //连接成功后就去找出该设备中的服务 private BluetoothGatt mBluetoothGatt;
			} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {  //连接失败
			}
		}
		@Override  //当设备是否找到服务时,会回调该函数
		public void onServicesDiscovered(BluetoothGatt gatt, int status) {
			if (status == BluetoothGatt.GATT_SUCCESS) {   //找到服务了
				//在这里可以对服务进行解析,寻找到你需要的服务
			} else {
				Log.w(TAG, "onServicesDiscovered received: " + status);
			}
		}
		@Override  //当读取设备时会回调该函数
		public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {
			System.out.println("onCharacteristicRead");
			if (status == BluetoothGatt.GATT_SUCCESS) {
			  //读取到的数据存在characteristic当中,可以通过characteristic.getValue();函数取出。然后再进行解析操作。
                          //int charaProp = characteristic.getProperties();if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0)表示可发出通知。  判断该Characteristic属性
			}
		}
		@Override //当向设备Descriptor中写数据时,会回调该函数
		public void onDescriptorWrite(BluetoothGatt gatt,BluetoothGattDescriptor descriptor, int status) {
			System.out.println("onDescriptorWriteonDescriptorWrite = " + status + ", descriptor =" + descriptor.getUuid().toString());
		}
		@Override //设备发出通知时会调用到该接口
		public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) {
			if (characteristic.getValue() != null) {
			      System.out.println(characteristic.getStringValue(0));
			}
			System.out.println("--------onCharacteristicChanged-----");
		}
		public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
			System.out.println("rssi = " + rssi);
		}
                @Override //当向Characteristic写数据时会回调该函数
                public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {
                           System.out.println("--------write success----- status:" + status);
                };
}

9:设备连接成功并回调BluetoothGattCallback接口里面的onConnectionStateChange函数,然后调用mBluetoothGatt.discoverServices();去发现服务。发现服务后会回调BluetoothGattCallback接口里面的 onServicesDiscovered函数,在里面我们可以获取服务列表。

public List<BluetoothGattService> getSupportedGattServices() {
		if (mBluetoothGatt == null)
			return null;
		return mBluetoothGatt.getServices();   //此处返回获取到的服务列表
	}

10:获取到服务列表后自然就是要对服务进行解析。解析出有哪些服务,服务里有哪些Characteristic,哪些Characteristic可读可写可发通知等等。

private void displayGattServices(List<BluetoothGattService> gattServices) {
		if (gattServices == null)
			return;
		for (BluetoothGattService gattService : gattServices) { // 遍历出gattServices里面的所有服务
			List<BluetoothGattCharacteristic> gattCharacteristics = gattServices.getCharacteristics();
			for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { // 遍历每条服务里的所有Characteristic
				if (gattCharacteristic.getUuid().toString().equalsIgnoreCase(需要通信的UUID)) { 
                                        // 有哪些UUID,每个UUID有什么属性及作用,一般硬件工程师都会给相应的文档。我们程序也可以读取其属性判断其属性。
					// 此处可以可根据UUID的类型对设备进行读操作,写操作,设置notification等操作
					// BluetoothGattCharacteristic gattNoticCharacteristic 假设是可设置通知的Characteristic
					// BluetoothGattCharacteristic gattWriteCharacteristic 假设是可读的Characteristic
					// BluetoothGattCharacteristic gattReadCharacteristic  假设是可写的Characteristic
				}
			}
		}
	}

11:可接收通知的UUID,设置其可以接收通知(notification)。下面函数参数为10中的gattNoticCharacteristic

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
		if (mBluetoothAdapter == null || mBluetoothGatt == null) {
			Log.w(TAG, "BluetoothAdapter not initialized");
			return;
		}
		mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
		BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID
				.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
		if (descriptor != null) {
			System.out.println("write descriptor");
			descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
			mBluetoothGatt.writeDescriptor(descriptor);
		}
}

12:可读的UUID。下面函数参数为10中的gattReadCharacteristic。readCharacteristic调用成功会回调步骤8中的onCharacteristicRead函数

public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
		if (mBluetoothAdapter == null || mBluetoothGatt == null) {
			Log.w(TAG, "BluetoothAdapter not initialized");
			return;
		}
		mBluetoothGatt.readCharacteristic(characteristic);
	}

13:可写的UUID。下面函数参数为10中的gattWriteCharacteristic。writeCharacteristic调用成功会回调步骤8中的onCharacteristicWrite函数

public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) {
		if (mBluetoothAdapter == null || mBluetoothGatt == null) {
			Log.w(TAG, "BluetoothAdapter not initialized");
			return;
		}
		mBluetoothGatt.writeCharacteristic(characteristic);
	}
  • 6
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值