private void connectLeDevice(final String mac){
//获取蓝牙适配器
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter == null){
Log.d("connectDevice","bluetoothAdapter is null");
return;
}
//根据mac地址获取蓝牙设备实例对象,当然此处的blueDevcie也可以通过蓝牙扫描得到
BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice(mac);
if(bluetoothDevice == null){
Log.d("connectDevice","the device is not exist");
return;
}
gattCallback=new BluetoothGattCallback() {
/**
* 断开或连接 状态发生变化时调用
* */
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
Log.e(TAG,"onConnectionStateChange()");
if (status==BluetoothGatt.GATT_SUCCESS){
//连接成功
if (newState== BluetoothGatt.STATE_CONNECTED){
Log.e(TAG,"连接成功");
//发现服务
gatt.discoverServices();
}else{
//断开连接之后的操作
}
}else{
//连接失败
Log.e(TAG,"失败=="+status);
mBluetoothGatt.close();
mBluetoothGatt.disconnect();
isConnecting=false;
}
}
/**
* 发现设备(真正建立连接)
* */
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
//直到这里才是真正建立了可通信的连接
isConnecting=false;
Log.e(TAG,"onServicesDiscovered()---建立连接");
//获取初始化服务和特征值
initServiceAndChara();
//订阅通知
mBluetoothGatt.setCharacteristicNotification(mBluetoothGatt
.getService(notify_UUID_service).getCharacteristic(notify_UUID_chara),true);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run(){
bleListView.setVisibility(View.GONE);
operaView.setVisibility(View.VISIBLE);
tvSerBindStatus.setText("已连接");
}
});
}
/**
* 读操作的回调
* */
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
Log.e(TAG,"onCharacteristicRead()");
}
/**
* 写操作的回调
* */
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.e(TAG,"onCharacteristicWrite() status="+status+",value="+HexUtil.encodeHexStr(characteristic.getValue()));
}
/**
* 接收到硬件返回的数据
* */
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.e(TAG,"onCharacteristicChanged()"+ Arrays.toString(characteristic.getValue()));
final byte[] data=characteristic.getValue();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
addText(tvResponse,bytes2hex(data));
}
});
}
};
//获取当前gatt服务下连接的设备
if(mBluetoothGatt != null && mBluetoothGatt.getConnectedDevices() != null){
for(BluetoothDevice device : mBluetoothGatt.getConnectedDevices()){
if(device.getAddress().equals(mac)){ // 如果当前遍历出的连接设备与我们需要连得设备是同一设备
mBluetoothGatt.disconnect(); //先去断开之前未正常断开的连接,解决连接133的问题
}
}
}
mBluetoothGatt.close(); //释放Gatt服务
mBluetoothGatt = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mBluetoothGatt = bluetoothDevice.connectGatt(getContext(),false,gattCallback,TRANSPORT_LE);
}else {
mBluetoothGatt = bluetoothDevice.connectGatt(getContext(),false,gattCallback);
}
}
bleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG,"已点击ListView");
if (isScaning){
stopScanDevice();
}
if (!isConnecting){
isConnecting=true;
BluetoothDevice bluetoothDevice= mDatas.get(position);
//连接设备
tvSerBindStatus.setText("连接中");
connectLeDevice(bluetoothDevice.getAddress());
}
Log.d(TAG,"退出ListVIew点击事件");
}
});
做一个BLE蓝牙开发,能够扫描显示设备到ListView,但是点击设备进行连接立马闪退