BluetoothGattCallback
private BluetoothGattCallback GattCallback = new BluetoothGattCallback() {
// 这里有9个要实现的方法,看情况要实现那些,用到那些就实现那些
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState){};
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){};
};
@SuppressLint("NewApi")
public class BLEActivity extends Activity {
private static Context mContext;// 上下文
private static final String TAG = BLEActivity.class.getSimpleName();// 标志
private static final boolean AUTO_CONNECT = true;// 是否自动连接
private static final boolean NOTIFICATION_ENABLED = true;
private int mConnectionState = STATE_DISCONNECTED;// 连接状态
private static final int STATE_DISCONNECTED = 0;// 断开连接
private static final int STATE_CONNECTING = 1;// 连接中
private static final int STATE_CONNECTED = 2;// 已连接
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
init();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
suiside();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
// 如果地址不为空就尝试连接 如果为空就跳到BLEActivity让用户去选择BLE设备
if (mBluetoothDeviceAddress != null) {
if (connect(mBluetoothDeviceAddress)) {
} else {
}
} else {
Intent bleIntent = new Intent();
bleIntent.setClass(mContext, BLEActivity.class);
bleIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(bleIntent);
}
// return super.onStartCommand(intent, flags, startId);
return Service.START_STICKY;
}
/**
* GATT通信回調函數
*/
@SuppressLint("NewApi")
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
// TODO Auto-generated method stub
// super.onCharacteristicChanged(gatt, characteristic);
System.out.println("我收到的:" + new String(characteristic.getValue()));
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
// TODO Auto-generated method stub
super.onCharacteristicRead(gatt, characteristic, status);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
// TODO Auto-generated method stub
super.onCharacteristicWrite(gatt, characteristic, status);
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
// TODO Auto-generated method stub
// super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
if (mBluetoothGatt != null)
mBluetoothGatt.discoverServices();
mConnectionState = STATE_CONNECTED;
System.out.println("state connected");
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
// connect(mBluetoothDeviceAddress);
mConnectionState = STATE_DISCONNECTED;
System.out.println("state disconnected");
}
}
@Override
public void onDescriptorRead(BluetoothGatt gatt,
BluetoothGattDescriptor descriptor, int status) {
// TODO Auto-generated method stub
super.onDescriptorRead(gatt, descriptor, status);
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt,
BluetoothGattDescriptor descriptor, int status) {
// TODO Auto-generated method stub
super.onDescriptorWrite(gatt, descriptor, status);
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
// TODO Auto-generated method stub
super.onReadRemoteRssi(gatt, rssi, status);
}
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
// TODO Auto-generated method stub
super.onReliableWriteCompleted(gatt, status);
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
// TODO Auto-generated method stub
// super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
if (mBluetoothGatt != null) {
BluetoothGattService mGattService = mBluetoothGatt
.getService(SampleGattAttributes.UUID_SERVICE);
mCharacteristic = mGattService
.getCharacteristic(SampleGattAttributes.UUID_CHARACTERISTIC);
List<BluetoothGattDescriptor> mDescriptors = mCharacteristic
.getDescriptors();
for (BluetoothGattDescriptor mDescriptor : mDescriptors) {
System.out.println(mDescriptor.getUuid().toString());
}