package com.nuoda.uniapp.Application;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
/**
*
*
*
*
* */
public class BluetoothManager {
private BluetoothManager(){ }
private static BluetoothManager mBluetoothManager;
public static BluetoothManager getBluetoothManager(){
if (mBluetoothManager==null)mBluetoothManager = new BluetoothManager();
return mBluetoothManager;
}
private BluetoothGatt bluetoothGatt = null;
private BluetoothGattCharacteristic myCharacteristic = null;
private String bleValue = "....";
private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private final BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
private UUID YOUR_SERVICE_UUID = UUID.fromString("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
private UUID YOUR_CHARACTERISTIC_UUID = UUID.fromString("beb5483e-36e1-4688-b7f5-ea07361b26a8");
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
openNotification();
}
@SuppressLint("MissingPermission")
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
//连接成功开启服务
if (newState == BluetoothProfile.STATE_CONNECTED) {
gatt.discoverServices();
}else {
disconnect();
}
}
//当 BLE 设备的服务被发现时调用。这是在连接成功后进行服务发现时触发的回调
@SuppressLint("MissingPermission")
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
gatt.requestMtu(300);
BluetoothGattService service = gatt.getService(YOUR_SERVICE_UUID);
if (service!=null){
myCharacteristic = service.getCharacteristic(YOUR_CHARACTERISTIC_UUID);
bluetoothGatt = gatt;
}
}
}
//当特性值发生变化时调用。这通常是因为设备向手机推送了新的数据
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){
bluetoothGatt = gatt;
myCharacteristic = characteristic;
getString();
}
//当读取特性值的操作完成时调用。此时可以获取特性的值
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS){
bluetoothGatt = gatt;
myCharacteristic = characteristic;
getString();
}
}
//当写入特性值的操作完成时调用
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
// 检查写入操作是否成功
if (status == BluetoothGatt.GATT_SUCCESS) {
bluetoothGatt = gatt;
myCharacteristic = characteristic;
getString();
}
}
};
@SuppressLint("MissingPermission")
private void openNotification(){
if(bluetoothGatt==null || myCharacteristic==null)return;
try{
//开启通知
bluetoothGatt.setCharacteristicNotification(myCharacteristic, true);
BluetoothGattDescriptor descriptor = myCharacteristic.getDescriptors().get(0);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
}catch (Exception e){
Log.d("bleerror:",e.getMessage());
}
//主动获取特征值
read();
}
@SuppressLint("MissingPermission")
public List getDivList(){
List listData = new ArrayList<>();
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
// 蓝牙不可用
}else {
Set pairedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice bean:pairedDevices)listData.add(bean);
}
return listData;
}
@SuppressLint("MissingPermission")
public void connect(Context context, BluetoothDevice device) {
if (bluetoothGatt!=null)return;
bluetoothGatt = device.connectGatt(context, false, bluetoothGattCallback);
}
//读取
@SuppressLint("MissingPermission")
public void read(){
if (bluetoothGatt==null || myCharacteristic==null)return;
bluetoothGatt.readCharacteristic(myCharacteristic);
}
//写入
@SuppressLint("MissingPermission")
public void write(String value){
if (bluetoothGatt==null || myCharacteristic==null)return;
myCharacteristic.setValue(value.getBytes());
bluetoothGatt.writeCharacteristic(myCharacteristic);
}
@SuppressLint("MissingPermission")
public void disconnect() {
try {
if (bluetoothGatt != null) {
bluetoothGatt.disconnect();
bluetoothGatt.close();
bluetoothGatt = null;
myCharacteristic=null;
}
} catch (Exception e) {
// 处理异常关闭
}
}
public boolean isble(){
return bluetoothGatt!=null;
}
public String getValue(){
return bleValue;
}
public void getString(){
if (myCharacteristic==null)return;
byte[] data = myCharacteristic.getValue();
if (data==null)return;
StringBuilder sb = new StringBuilder();
for (byte b:data) sb.append((char) (b & 0xFF));
bleValue = sb.toString();
if (bleValue==null)bleValue="";
if("r:not rfid".equals(bleValue)){
bleValue = "未识别到ic卡";
}else if ("r:rfid error".equals(bleValue)){
bleValue = "操作ic卡异常";
}else if ("r:write ok".equals(bleValue)){
bleValue = "写入成功";
}else if (bleValue.indexOf("r:")==0){
bleValue = "ic卡读取数据:" + bleValue.substring(2);
}else if ("c:...".equals(bleValue)){
bleValue = "扫码头已打开";
}else if ("c:end".equals(bleValue)){
bleValue = "扫码头已关闭";
}else if (bleValue.indexOf("c:")==0){
bleValue = "扫码数据:" + bleValue.substring(2);
}
Log.d("bledata:",bleValue);
}
}