注:如果复制代码进项目时显示红色,请按ALT+ENTER键导包(import class)
package club.stm32;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
/**
* 需要根据条件修改的参数
*/
private String MacHeader = "A5:B3:C2"; //蓝牙的Mac地址前六位,需要根据自己的蓝牙进行修改
private String SERVICESUUID = "0000ff00-0000-1000-8000-00805f9b34fb"; //服务的UUID
private String WRITEUUID = "0000ff02-0000-1000-8000-00805f9b34fb"; //写入特征的UUID
private String NOTIFYUUID = "0000ff01-0000-1000-8000-00805f9b34fb"; //监听特征的UUID
private BytesHexStrTranslate bytesHexStrTranslate;
private BluetoothAdapter bluetoothAdapter;
private Button btnCheckPermission;
private TextView tvmsg;
private Button btnSearchBLE;
private BluetoothDevice mdevice;
private BluetoothGatt bluetoothGatt;
private TextView getSendText;
private Button btnSend;
private BluetoothGattService bluetoothGattService;
private BluetoothGattCharacteristic writeCharacteristic;
private BluetoothGattCharacteristic notifyCharacteristic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPermission(); //获取权限
bluetoothInit(); //蓝牙初始化
widgetInit(); //控件初始化
widgetListener(); //控件监听
}
//获取权限
private void getPermission() {
//如果sdk版本大于23
if (Build.VERSION.SDK_INT >=23){
//如果没有权限
if ((ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED))
{
//动态申请权限
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 10);
}
}
}
//控件初始化
private void widgetInit() {
//请自行提升到全局,原型是:Button startscan = findViewById(R.id.startscan);
btnCheckPermission = findViewById(R.id.btnCheckPermission);
//请自行提升到全局,原型是:TextView tvmsg = findViewById(R.id.tvmsg);
tvmsg = findViewById(R.id.tvmsg);
//请自行提升到全局,原型是:Button btnSearchBLE = findViewById(R.id.btnSearchBLE);
btnSearchBLE = findViewById(R.id.btnSearchBLE);
//请自行提升到全局,原型是:TextView getSendText = findViewById(R.id.getSendText);
getSendText = findViewById(R.id.getSendText);
//请自行提升到全局,原型是:Button btnSend = findViewById(R.id.btnSend);
btnSend = findViewById(R.id.btnSend);
}
//控件监听
private void widgetListener() {
//测试权限按钮监听
btnCheckPermission.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {//如果有权限
Toast.makeText(MainActivity.this, "hava this permission", Toast.LENGTH_SHORT).show();//toast信息
Log.d("权限:","有定位权限");//在logcat上打印信息
tvmsg.setText("有定位权限");
}else {
getPermission();//获取权限
Toast.makeText(MainActivity.this, "no this permission", Toast.LENGTH_SHORT).show();//toast信息
Log.d("权限:","无定位权限");//在logcat上打印信息
tvmsg.setText("无定位权限");
}
}
});
//搜索蓝牙按钮监听
btnSearchBLE.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//开始搜索蓝牙
bluetoothAdapter.startLeScan(mBLEScanCallback);
}
});
//
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String res = getSendText.getText().toString();
Log.d("发送的数据是",res);
byte[] mybyte = bytesHexStrTranslate.StringtoBytes(res);
writeCharacteristic.setValue(mybyte);
writeCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
bluetoothGatt.writeCharacteristic(writeCharacteristic);
}
});
}
//mBLEScanCallback回调函数
private BluetoothAdapter.LeScanCallback mBLEScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
//打印蓝牙mac地址
Log.d("BleMAC", device.getAddress());
if(device.toString().indexOf(MacHeader) == 0){
Log.d("符合的蓝牙地址", device.getAddress());
bluetoothAdapter.stopLeScan(mBLEScanCallback);
//停止搜索蓝牙,降低功耗
bluetoothAdapter.stopLeScan(mBLEScanCallback);
//获取远程设备(连接蓝牙)原型是BluetoothDevice mdevice = bluetoothAdapter.getRemoteDevice(des);请自行提升全局
mdevice = bluetoothAdapter.getRemoteDevice(device.toString());
//连接bluetoothGatt 到这一步时,蓝牙已经连接上了
//原型是BluetoothGatt bluetoothGatt = device.connectGatt(MainActivity.this, false, bluetoothGattCallback); 请自行提升全局
//bluetoothGattCallback是蓝牙gatt回调函数,接下来会跳到bluetoothGattCallback函数
bluetoothGatt = mdevice.connectGatt(MainActivity.this, false, bluetoothGattCallback);
}
}
};
//蓝牙回调函数
private final BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
//连接状态改变时回调
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
bluetoothGatt.discoverServices();
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.d("onConnectionStateChange","连接成功");
}
else {
Log.d("onConnectionStateChange","连接断开");
}
}
//写入成功回调函数
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.d("onCharacteristicWrite", "写入成功");
}
//UUID搜索成功回调
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d("onCharacteristicWrite", "写入成功");
if (status == BluetoothGatt.GATT_SUCCESS) {
List supportedGattServices = bluetoothGatt.getServices();
for (int i = 0; i < supportedGattServices.size(); i++) {
Log.i("success", "1:BluetoothGattService UUID=:" + supportedGattServices.get(i).getUuid());
List listGattCharacteristic = supportedGattServices.get(i).getCharacteristics();
for (int j = 0; j < listGattCharacteristic.size(); j++) {
Log.i("success", "2: BluetoothGattCharacteristic UUID=:" + listGattCharacteristic.get(j).getUuid());
}
}
} else {
Log.e("fail", "onservicesdiscovered收到: " + status);
}
//设置serviceUUID,原型是:BluetoothGattService bluetoothGattService = bluetoothGatt.getService(UUID.fromString(SERVICESUUID));
bluetoothGattService = bluetoothGatt.getService(UUID.fromString(SERVICESUUID));
//设置写入特征UUID,原型是:BluetoothGattCharacteristic writeCharacteristic = bluetoothGattService.getCharacteristic(UUID.fromString(WRITEUUID));
writeCharacteristic = bluetoothGattService.getCharacteristic(UUID.fromString(WRITEUUID));
//设置监听特征UUID,原型是:BluetoothGattCharacteristic notifyCharacteristic = bluetoothGattService.getCharacteristic(UUID.fromString(NOTIFYUUID));
notifyCharacteristic = bluetoothGattService.getCharacteristic(UUID.fromString(NOTIFYUUID));
//开启监听
gatt.setCharacteristicNotification(notifyCharacteristic,true);
Log.d("uuidconnectsuccess", "uuid连接成功");
}
//接受数据回调
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
byte[] value = characteristic.getValue();
Log.e("ReceiveSuccess", bytesHexStrTranslate.bytesToHexFun1(value));
}
};
private void bluetoothInit() {
//如果不支持蓝牙
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
{
//提示不支持蓝牙
Toast.makeText(this, "程序不支持该设备", Toast.LENGTH_SHORT).show();
//退出程序
finish();
}
//创建蓝牙适配器原型是BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//如果蓝牙适配器为空
if (bluetoothAdapter == null)
{
//显示设备无蓝牙
Toast.makeText(this, "设备无蓝牙", Toast.LENGTH_SHORT).show();
//退出
finish();
}
//如果蓝牙未开启
if (!bluetoothAdapter.isEnabled())
{
//不提示,直接开启蓝牙
bluetoothAdapter.enable();
//提示开启蓝牙中
Toast.makeText(this, "开启蓝牙中,如果未开启,请检查应用权限", Toast.LENGTH_SHORT).show();
}
}
}
class BytesHexStrTranslate {
private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
public static String bytesToHexFun1(byte[] bytes) {
// 一个byte为8位,可用两个十六进制位标识
char[] buf = new char[bytes.length * 2];
int a = 0;
int index = 0;
for(byte b : bytes) { // 使用除与取余进行转换
if(b < 0) {
a = 256 + b;
} else {
a = b;
}
buf[index++] = HEX_CHAR[a / 16];
buf[index++] = HEX_CHAR[a % 16];
}
return new String(buf);
}
public static String bytesToHexFun2(byte[] bytes) {
char[] buf = new char[bytes.length * 2];
int index = 0;
for(byte b : bytes) { // 利用位运算进行转换,可以看作方法一的变种
buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
buf[index++] = HEX_CHAR[b & 0xf];
}
return new String(buf);
}
public static String bytesToHexFun3(byte[] bytes) {
StringBuilder buf = new StringBuilder(bytes.length * 2);
for(byte b : bytes) { // 使用String的format方法进行转换
buf.append(String.format("%02x", new Integer(b & 0xff)));
}
return buf.toString();
}
public static byte[] StringtoBytes(String str) {
if(str == null || str.trim().equals("")) {
return new byte[0];
}
byte[] bytes = new byte[str.length() / 2];
for(int i = 0; i < str.length() / 2; i++) {
String subStr = str.substring(i * 2, i * 2 + 2);
bytes[i] = (byte) Integer.parseInt(subStr, 16);
}
return bytes;
}
}
2019-04-24 16:51:09.683 14801-14833/club.stm32 I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8996.so from the current namespace instead.
2019-04-24 16:51:09.688 14801-14833/club.stm32 I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
2019-04-24 16:51:09.694 14801-14833/club.stm32 I/OpenGLRenderer: Initialized EGL, version 1.4
2019-04-24 16:51:09.694 14801-14833/club.stm32 D/OpenGLRenderer: Swap behavior 2
2019-04-24 16:51:09.760 14801-14833/club.stm32 I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8996.so from the current namespace instead.
2019-04-24 16:51:30.035 14801-14814/club.stm32 I/zygote64: Do partial code cache collection, code=25KB, data=29KB
2019-04-24 16:51:30.036 14801-14814/club.stm32 I/zygote64: After code cache collection, code=25KB, data=29KB
2019-04-24 16:51:30.036 14801-14814/club.stm32 I/zygote64: Increasing code cache capacity to 128KB
2019-04-24 16:51:39.678 14801-14801/club.stm32 D/Toast: Toast package name=club.stm32
2019-04-24 16:51:39.681 14801-14801/club.stm32 D/权限:: 有定位权限
2019-04-24 16:51:39.903 14801-14814/club.stm32 I/zygote64: Compiler allocated 6MB to compile void android.view.ViewRootImpl.performTraversals()
2019-04-24 16:51:39.926 14801-14814/club.stm32 I/zygote64: Do partial code cache collection, code=61KB, data=60KB
2019-04-24 16:51:39.927 14801-14814/club.stm32 I/zygote64: After code cache collection, code=61KB, data=60KB
2019-04-24 16:51:39.927 14801-14814/club.stm32 I/zygote64: Increasing code cache capacity to 256KB
2019-04-24 16:51:40.957 14801-14801/club.stm32 D/BluetoothAdapter: startLeScan(): null
2019-04-24 16:51:40.961 14801-14801/club.stm32 D/BluetoothAdapter: isLeEnabled(): ON
2019-04-24 16:51:40.966 14801-14827/club.stm32 D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=10 mScannerId=0
2019-04-24 16:51:41.031 14801-14801/club.stm32 D/BleMAC: 6A:F1:E0:40:B3:5F
2019-04-24 16:51:41.041 14801-14801/club.stm32 D/BleMAC: 7D:EE:71:16:22:4D
2019-04-24 16:51:41.079 14801-14801/club.stm32 D/BleMAC: A5:B3:C2:E3:00:01
2019-04-24 16:51:41.079 14801-14801/club.stm32 D/符合的蓝牙地址: A5:B3:C2:E3:00:01
2019-04-24 16:51:41.080 14801-14801/club.stm32 D/BluetoothAdapter: stopLeScan()
2019-04-24 16:51:41.083 14801-14801/club.stm32 D/BluetoothAdapter: isLeEnabled(): ON
2019-04-24 16:51:41.087 14801-14801/club.stm32 D/BluetoothAdapter: stopLeScan()
2019-04-24 16:51:41.089 14801-14801/club.stm32 D/BluetoothAdapter: scan not started yet
2019-04-24 16:51:41.092 14801-14801/club.stm32 D/BluetoothGatt: connect() - device: A5:B3:C2:E3:00:01, auto: false
2019-04-24 16:51:41.092 14801-14801/club.stm32 D/BluetoothGatt: registerApp()
2019-04-24 16:51:41.093 14801-14801/club.stm32 D/BluetoothGatt: registerApp() - UUID=bda24727-4e15-4b74-b6a7-01b809b5bd0f
2019-04-24 16:51:41.098 14801-14801/club.stm32 D/BleMAC: 1C:0F:04:00:E3:DD
2019-04-24 16:51:41.098 14801-14827/club.stm32 D/BluetoothGatt: onClientRegistered() - status=0 clientIf=10
2019-04-24 16:51:43.766 14801-14827/club.stm32 D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=10 device=A5:B3:C2:E3:00:01
2019-04-24 16:51:43.767 14801-14827/club.stm32 D/BluetoothGatt: discoverServices() - device: A5:B3:C2:E3:00:01
2019-04-24 16:51:43.772 14801-14827/club.stm32 D/onConnectionStateChange: 连接成功
2019-04-24 16:51:44.164 14801-14827/club.stm32 D/BluetoothGatt: onConnectionUpdated() - Device=A5:B3:C2:E3:00:01 interval=6 latency=0 timeout=500 status=0
2019-04-24 16:51:44.733 14801-14827/club.stm32 D/BluetoothGatt: onSearchComplete() = Device=A5:B3:C2:E3:00:01 Status=0
2019-04-24 16:51:44.734 14801-14827/club.stm32 D/onCharacteristicWrite: 写入成功
2019-04-24 16:51:44.735 14801-14827/club.stm32 I/success: 1:BluetoothGattService UUID=:00001800-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.736 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:00002a00-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.737 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:00002a01-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.737 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:00002a04-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.738 14801-14827/club.stm32 I/success: 1:BluetoothGattService UUID=:00001801-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.738 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:00002a05-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.739 14801-14827/club.stm32 I/success: 1:BluetoothGattService UUID=:0000180a-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.740 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:00002a50-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.740 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:00002a29-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.741 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:00002a24-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.742 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:00002a26-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.742 14801-14827/club.stm32 I/success: 1:BluetoothGattService UUID=:0000180f-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.742 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:00002a19-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.743 14801-14827/club.stm32 I/success: 1:BluetoothGattService UUID=:0000ff00-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.743 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:0000ff01-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.744 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:0000ff02-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.744 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:0000ff21-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.745 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:0000ff22-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.746 14801-14827/club.stm32 I/success: 1:BluetoothGattService UUID=:00010203-0405-0607-0809-0a0b0c0d1911
2019-04-24 16:51:44.747 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:00010203-0405-0607-0809-0a0b0c0d2b12
2019-04-24 16:51:44.747 14801-14827/club.stm32 I/success: 1:BluetoothGattService UUID=:0000fee7-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.747 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:0000fec7-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.748 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:0000fec8-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.748 14801-14827/club.stm32 I/success: 2: BluetoothGattCharacteristic UUID=:0000fec9-0000-1000-8000-00805f9b34fb
2019-04-24 16:51:44.752 14801-14827/club.stm32 D/BluetoothGatt: setCharacteristicNotification() - uuid: 0000ff01-0000-1000-8000-00805f9b34fb enable: true
2019-04-24 16:51:44.754 14801-14827/club.stm32 D/uuidconnectsuccess: uuid连接成功
2019-04-24 16:51:44.767 14801-14827/club.stm32 D/BluetoothGatt: onConnectionUpdated() - Device=A5:B3:C2:E3:00:01 interval=12 latency=0 timeout=300 status=0
2019-04-24 16:52:04.332 14801-14814/club.stm32 I/zygote64: Do full code cache collection, code=124KB, data=103KB
2019-04-24 16:52:04.332 14801-14814/club.stm32 I/zygote64: After code cache collection, code=116KB, data=76KB
2019-04-24 16:52:04.963 14801-14801/club.stm32 D/发送的数据是: 1111
2019-04-24 16:52:04.966 14801-14827/club.stm32 D/onCharacteristicWrite: 写入成功
2019-04-24 16:52:06.653 14801-14801/club.stm32 D/发送的数据是: 1111
2019-04-24 16:52:06.655 14801-14827/club.stm32 D/onCharacteristicWrite: 写入成功
2019-04-24 16:52:13.279 14801-14827/club.stm32 E/ReceiveSuccess: 2222
2019-04-24 16:52:15.245 14801-14827/club.stm32 E/ReceiveSuccess: 2222
2019-04-24 16:52:34.723 14801-14814/club.stm32 I/zygote64: Do partial code cache collection, code=124KB, data=90KB
2019-04-24 16:52:34.725 14801-14814/club.stm32 I/zygote64: After code cache collection, code=124KB, data=90KB
2019-04-24 16:52:34.725 14801-14814/club.stm32 I/zygote64: Increasing code cache capacity to 512KB