<!-- 声明蓝牙权限 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
package com.example.bletest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
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.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private String TAG = "ldq";
private BluetoothManager bluetoothmanager;
private BluetoothAdapter mBluetoothAdapter;
private final static String TX_POWER = "00001804-0000-1000-8000-00805f9b34fb";
private final static String TX_POWER_LEVEL = "00002a07-0000-1000-8000-00805f9b34fb";
private final static String AUDIO_CHANGE_SERVICE = "00001800-0000-1000-8000-00805f9b34fb";
private final static String AUDIO_CHANGE_CS = "00002a00-0000-1000-8000-00805f9b34fb";
private BluetoothGatt mBluetoothGatt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothmanager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothmanager.getAdapter();
BluetoothDevice mBluetoothDevice = mBluetoothAdapter.getRemoteDevice( "9C:06:6E:F6:DE:AB" );
Button button = ( Button )findViewById( R.id.connect );
BleGattCallback bleGattCallback = new BleGattCallback();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mBluetoothGatt = mBluetoothDevice.connectGatt( MainActivity.this,true, bleGattCallback);
}
});
}
private void writeValue( byte[] data){
BluetoothGattService service = mBluetoothGatt.getService( UUID.fromString(AUDIO_CHANGE_SERVICE) );
BluetoothGattCharacteristic cs = service.getCharacteristic( UUID.fromString(AUDIO_CHANGE_CS) );
cs.setValue(data);
mBluetoothGatt.writeCharacteristic( cs );
}
private Handler mHandler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
switch ( msg.what ){
case 111:
BluetoothGattService service = mBluetoothGatt.getService(UUID.fromString(TX_POWER));
if( service != null ){
BluetoothGattCharacteristic cs = service.getCharacteristic(UUID.fromString(TX_POWER_LEVEL));
mBluetoothGatt.readCharacteristic(cs);
}
break;
}
}
};
public class BleGattCallback extends BluetoothGattCallback {
@Override
public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
super.onPhyUpdate(gatt, txPhy, rxPhy, status);
Log.i(TAG,"回调触发的结果 setPreferredPhy(int, int, int),或者由于远程设备改变了体育。");
}
@Override
public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
super.onPhyRead(gatt, txPhy, rxPhy, status);
Log.i(TAG,"回调触发的结果 readPhy()");
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
Log.i(TAG,"连接状态回调");
if (status == BluetoothGatt.GATT_SUCCESS){
if (newState == BluetoothProfile.STATE_CONNECTED) {
gatt.discoverServices();
Log.e( TAG, "发现服务!" );
}else if(newState == BluetoothProfile.STATE_DISCONNECTED){
}
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.i(TAG,"服务发现回调");
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
Log.i(TAG,"当成功读取特征值时触发");
if (status == BluetoothGatt.GATT_SUCCESS) {
byte[] value = characteristic.getValue();
String str = byte2HexStr(value);
Log.e(TAG, "onCharacteristicRead:" + str);
}
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
byte[] value = characteristic.getValue();
String str = byte2HexStr(value);
Log.i(TAG,"当成功写入特征值到外设时触发 ");
Log.e( TAG,"写入: "+str );
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.i(TAG,"回调通知由于远程触发特征");
byte[] value = characteristic.getValue();
String str = byte2HexStr(value);
Log.e( "ldq", "str ="+str );
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
Log.i(TAG,"回调报告描述符读操作的结果");
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
Log.i(TAG,"回调描述符写操作的结果");
}
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
super.onReliableWriteCompleted(gatt, status);
Log.i(TAG,"回调时调用一个可靠写事务已经完成");
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
Log.i(TAG,"回调报告的RSSI远程设备连接。");
}
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
Log.i(TAG,"回调表明MTU对于一个给定的设备连接已经改变了");
}
}
public String byte2HexStr(byte[] b) {
if (b == null){
return null;
}
String stmp = "";
StringBuilder sb = new StringBuilder("");
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0xFF);
sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
sb.append(" ");
}
return sb.toString().toUpperCase().trim();
}
}