BLELinkUtil.DEBUGMODE = true;
bleLinkUtil = new BLELinkUtil(this);

连接蓝牙

bleLinkUtil.linkDevice(bluetoothDevice.getAddress(), new OnBLEFindServiceListener() {
@Override
public void onLink(boolean isSuccess, final List<BluetoothGattService> services) {
bleLinkUtil.setUUID(MSConstant.COM_SERVICE_UUID, MSConstant.TX_CHAR_UUID);
MSLogUtil.e("连接成功");
}
});

接收通知

bleLinkUtil.startGetNotification(MSConstant.COM_SERVICE_UUID.toString(), MSConstant.RX_CHAR_UUID.toString(), new OnBLENotificationListener() {
@Override
public void onGetData(String data) {
MSLogUtil.e("onReadMessage: " + data);
if (data.contains("CONFIG_SUCCESS")){


}

}
});

发送消息

private void sendConfigWifiCmd() {
String wifiSsid = btn_wifi_name.getText().toString();
String wifiPwd = et_wifi_pwd.getText().toString();
String serverIp = btn_server_ip.getText().toString();
String serverPort = et_server_port.getText().toString();
JSONObject cmdJson = new JSONObject();
cmdJson.put("cmd", "wifi_config");
cmdJson.put("wifiSsid", wifiSsid);
cmdJson.put("wifiPwd", wifiPwd);
cmdJson.put("serverIp", serverIp);
cmdJson.put("serverPort", serverPort);
bleLinkUtil.write(cmdJson.toJSONString(), new OnBLEWriteListener() {
@Override
public boolean onWrite(boolean isSuccess, String readStr) {
Log.e(">>>", "onWrite: " + readStr);
if (isSuccess) {
MSLogUtil.e("发送成功");
} else {
MSLogUtil.e("发送失败");
}
return true;
}
});
}

参考github

​https://github.com/kongzue/BTLinker​