当前位置:   article > 正文

基于Android Studio实现蓝牙组网_androidstudio蓝牙开发

androidstudio蓝牙开发

参考文章:http://t.csdnimg.cn/hVDdT

Android-蓝牙开发:解决搜索不到蓝牙设备_android项目中无法搜索到蓝牙-CSDN博客

第一篇是参考代码来源,第二篇是解决第一篇出现的搜索不到设备的问题。

演示视频:【第三步:成功啦!】 https://www.bilibili.com/video/BV1KJ4m1A7LQ/?share_source=copy_web&vd_source=feda4a3e701cae91ddc70f622b5b48d6

纠正视频里面的话,原帖主肯定是明白原理,但是我本人是照抄代码,没有搞清楚原理,因此是我自己的问题,如果有明白为什么会出现我视频里面问题的朋友可以留言告知我,我一定改正!! 


根据第一篇文章提供的源码链接下载后复制到自己的项目中,解决各种报错,并在主代码中添加第二篇文章提供的代码并调用getPermission()后,即可运行。


activity

bluet.java

  1. package com.example.yueduqi;
  2. import android.annotation.SuppressLint;
  3. import android.bluetooth.BluetoothAdapter;
  4. import android.bluetooth.BluetoothDevice;
  5. import android.bluetooth.BluetoothSocket;
  6. import android.content.BroadcastReceiver;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.IntentFilter;
  10. import android.content.pm.PackageManager;
  11. import android.os.Build;
  12. import android.os.Bundle;
  13. import android.os.Handler;
  14. import android.os.Message;
  15. import android.text.TextUtils;
  16. import android.util.Log;
  17. import android.view.View;
  18. import android.widget.AdapterView;
  19. import android.widget.Button;
  20. import android.widget.EditText;
  21. import android.widget.LinearLayout;
  22. import android.widget.ListView;
  23. import android.widget.TextView;
  24. import android.widget.Toast;
  25. import com.example.yueduqi.util.ClsUtils;
  26. import androidx.appcompat.app.AppCompatActivity;
  27. import androidx.core.app.ActivityCompat;
  28. public class bluet extends AppCompatActivity implements View.OnClickListener {
  29. private static final String TAG = "BtMain";
  30. private static final int CONNECT_SUCCESS = 0x01;
  31. private static final int CONNECT_FAILURE = 0x02;
  32. private static final int DISCONNECT_SUCCESS = 0x03;
  33. private static final int SEND_SUCCESS = 0x04;
  34. private static final int SEND_FAILURE = 0x05;
  35. private static final int RECEIVE_SUCCESS = 0x06;
  36. private static final int RECEIVE_FAILURE = 0x07;
  37. private static final int START_DISCOVERY = 0x08;
  38. private static final int STOP_DISCOVERY = 0x09;
  39. private static final int DISCOVERY_DEVICE = 0x0A;
  40. private static final int DEVICE_BOND_NONE = 0x0B;
  41. private static final int DEVICE_BONDING = 0x0C;
  42. private static final int DEVICE_BONDED = 0x0D;
  43. private Button btSearch;
  44. private TextView tvCurConState;
  45. private TextView tvCurBondState;
  46. private TextView tvName;
  47. private TextView tvAddress;
  48. private Button btConnect;
  49. private Button btDisconnect;
  50. private Button btBound;
  51. private Button btDisBound;
  52. private EditText etSendMsg;
  53. private Button btSend;
  54. private TextView tvSendResult;
  55. private TextView tvReceive;
  56. private LinearLayout llDeviceList;
  57. private LinearLayout llDataSendReceive;
  58. private ListView lvDevices;
  59. private LVDevicesAdapter lvDevicesAdapter;
  60. //蓝牙
  61. private BluetoothAdapter bluetoothAdapter;
  62. private BtBroadcastReceiver btBroadcastReceiver;
  63. //连接设备的UUID
  64. public static final String MY_BLUETOOTH_UUID = "00001101-0000-1000-8000-00805F9B34FB"; //蓝牙通讯
  65. //当前要连接的设备
  66. private BluetoothDevice curBluetoothDevice;
  67. //发起连接的线程
  68. private ConnectThread connectThread;
  69. //管理连接的线程
  70. private ConnectedThread connectedThread;
  71. //当前设备连接状态
  72. private boolean curConnState = false;
  73. //当前设备与系统配对状态
  74. private boolean curBondState = false;
  75. @SuppressLint("HandlerLeak")
  76. private Handler mHandler = new Handler() {
  77. @Override
  78. public void handleMessage(Message msg) {
  79. super.handleMessage(msg);
  80. switch (msg.what) {
  81. case START_DISCOVERY:
  82. Log.d(TAG, "开始搜索设备...");
  83. break;
  84. case STOP_DISCOVERY:
  85. Log.d(TAG, "停止搜索设备...");
  86. break;
  87. case DISCOVERY_DEVICE: //扫描到设备
  88. BluetoothDevice bluetoothDevice = (BluetoothDevice) msg.obj;
  89. lvDevicesAdapter.addDevice(bluetoothDevice);
  90. break;
  91. case CONNECT_FAILURE: //连接失败
  92. Log.d(TAG, "连接失败");
  93. tvCurConState.setText("连接失败");
  94. curConnState = false;
  95. break;
  96. case CONNECT_SUCCESS: //连接成功
  97. Log.d(TAG, "连接成功");
  98. tvCurConState.setText("连接成功");
  99. curConnState = true;
  100. llDataSendReceive.setVisibility(View.VISIBLE);
  101. llDeviceList.setVisibility(View.GONE);
  102. break;
  103. case DISCONNECT_SUCCESS:
  104. tvCurConState.setText("断开成功");
  105. curConnState = false;
  106. break;
  107. case SEND_FAILURE: //发送失败
  108. Toast.makeText(bluet.this, "发送失败", Toast.LENGTH_SHORT).show();
  109. break;
  110. case SEND_SUCCESS: //发送成功
  111. String sendResult = (String) msg.obj;
  112. tvSendResult.setText(sendResult);
  113. break;
  114. case RECEIVE_FAILURE: //接收失败
  115. String receiveError = (String) msg.obj;
  116. tvReceive.setText(receiveError);
  117. break;
  118. case RECEIVE_SUCCESS: //接收成功
  119. String receiveResult = (String) msg.obj;
  120. tvReceive.setText(receiveResult);
  121. break;
  122. case DEVICE_BOND_NONE: //已解除配对
  123. tvCurBondState.setText("解除配对成功");
  124. curBondState = false;
  125. break;
  126. case DEVICE_BONDING: //正在配对
  127. tvCurBondState.setText("正在配对...");
  128. break;
  129. case DEVICE_BONDED: //已配对
  130. tvCurBondState.setText("配对成功");
  131. curBondState = true;
  132. break;
  133. }
  134. }
  135. };
  136. @Override
  137. protected void onCreate(Bundle savedInstanceState) {
  138. super.onCreate(savedInstanceState);
  139. setContentView(R.layout.bluetooth);
  140. //初始化视图
  141. initView();
  142. //初始化监听
  143. iniListener();
  144. //初始化数据
  145. initData();
  146. //初始化蓝牙
  147. initBluetooth();
  148. //初始化蓝牙广播
  149. initBtBroadcast();
  150. //安卓版本升级蓝牙连接需要获取位置
  151. getPermission();
  152. }
  153. private void initData() {
  154. lvDevicesAdapter = new LVDevicesAdapter(bluet.this);
  155. lvDevices.setAdapter(lvDevicesAdapter);
  156. }
  157. private void initView() {
  158. btSearch = findViewById(R.id.bt_search);
  159. tvCurConState = findViewById(R.id.tv_cur_con_state);
  160. tvCurBondState = findViewById(R.id.tv_cur_bond_state);
  161. btConnect = findViewById(R.id.bt_connect);
  162. btDisconnect = findViewById(R.id.bt_disconnect);
  163. btBound = findViewById(R.id.bt_bound);
  164. btDisBound = findViewById(R.id.bt_disBound);
  165. tvName = findViewById(R.id.tv_name);
  166. tvAddress = findViewById(R.id.tv_address);
  167. etSendMsg = findViewById(R.id.et_send_msg);
  168. btSend = findViewById(R.id.bt_to_send);
  169. tvSendResult = findViewById(R.id.tv_send_result);
  170. tvReceive = findViewById(R.id.tv_receive_result);
  171. llDeviceList = findViewById(R.id.ll_device_list);
  172. llDataSendReceive = findViewById(R.id.ll_data_send_receive);
  173. lvDevices = findViewById(R.id.lv_devices);
  174. }
  175. private void iniListener() {
  176. btSearch.setOnClickListener(this);
  177. btConnect.setOnClickListener(this);
  178. btDisconnect.setOnClickListener(this);
  179. btBound.setOnClickListener(this);
  180. btDisBound.setOnClickListener(this);
  181. btSend.setOnClickListener(this);
  182. lvDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  183. @Override
  184. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  185. BluetoothDevice bluetoothDevice = (BluetoothDevice) lvDevicesAdapter.getItem(i);
  186. if (ActivityCompat.checkSelfPermission(bluet.this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
  187. // TODO: Consider calling
  188. // ActivityCompat#requestPermissions
  189. // here to request the missing permissions, and then overriding
  190. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  191. // int[] grantResults)
  192. // to handle the case where the user grants the permission. See the documentation
  193. // for ActivityCompat#requestPermissions for more details.
  194. return;
  195. }
  196. tvName.setText(bluetoothDevice.getName());
  197. tvAddress.setText(bluetoothDevice.getAddress());
  198. curBluetoothDevice = bluetoothDevice;
  199. }
  200. });
  201. }
  202. @Override
  203. public void onClick(View view) {
  204. if (view.getId() == R.id.bt_search) { // 搜索蓝牙
  205. llDataSendReceive.setVisibility(View.GONE);
  206. llDeviceList.setVisibility(View.VISIBLE);
  207. searchBtDevice();
  208. } else if (view.getId() == R.id.bt_connect) { // 连接蓝牙
  209. if (!curConnState) {
  210. startConnectDevice(curBluetoothDevice, MY_BLUETOOTH_UUID, 10000);
  211. } else {
  212. Toast.makeText(this, "当前设备已连接", Toast.LENGTH_SHORT).show();
  213. }
  214. } else if (view.getId() == R.id.bt_disconnect) { // 断开连接
  215. if (curConnState) {
  216. clearConnectedThread();
  217. } else {
  218. Toast.makeText(this, "当前设备未连接", Toast.LENGTH_SHORT).show();
  219. }
  220. } else if (view.getId() == R.id.bt_bound) { // 配对设备
  221. if (!curBondState) {
  222. boundDevice(curBluetoothDevice);
  223. } else {
  224. Toast.makeText(this, "当前设备已经与系统蓝牙建立配对", Toast.LENGTH_SHORT).show();
  225. }
  226. } else if (view.getId() == R.id.bt_disBound) { // 解除配对
  227. if (curBondState) {
  228. disBoundDevice(curBluetoothDevice);
  229. } else {
  230. Toast.makeText(this, "当前设备尚未与系统蓝牙建立配对", Toast.LENGTH_SHORT).show();
  231. }
  232. } else if (view.getId() == R.id.bt_to_send) { // 发送数据
  233. if (curConnState) {
  234. String sendMsg = etSendMsg.getText().toString();
  235. if (sendMsg.isEmpty()) {
  236. Toast.makeText(this, "发送数据为空!", Toast.LENGTH_SHORT).show();
  237. return;
  238. }
  239. sendData(sendMsg, true); // 以16进制字符串形式发送数据
  240. } else {
  241. Toast.makeText(this, "请先连接当前设备", Toast.LENGTH_SHORT).show();
  242. }
  243. }
  244. // 注意:这里没有else块,因为如果没有匹配到任何ID,则不执行任何操作
  245. }
  246. /**
  247. * 解决:无法发现蓝牙设备的问题
  248. *
  249. * 对于发现新设备这个功能, 还需另外两个权限(Android M 以上版本需要显式获取授权,附授权代码):
  250. */
  251. private final int ACCESS_LOCATION=1;
  252. @SuppressLint("WrongConstant")
  253. private void getPermission() {
  254. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
  255. int permissionCheck = 0;
  256. permissionCheck = this.checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION);
  257. permissionCheck += this.checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION);
  258. if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
  259. //未获得权限
  260. this.requestPermissions( // 请求授权
  261. new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
  262. android.Manifest.permission.ACCESS_COARSE_LOCATION},
  263. ACCESS_LOCATION);// 自定义常量,任意整型
  264. }
  265. }
  266. }
  267. /**
  268. * 请求权限的结果回调。每次调用 requestpermissions(string[],int)时都会调用此方法。
  269. * @param requestCode 传入的请求代码
  270. * @param permissions 传入permissions的要求
  271. * @param grantResults 相应权限的授予结果:PERMISSION_GRANTED 或 PERMISSION_DENIED
  272. */
  273. @Override
  274. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  275. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  276. switch (requestCode) {
  277. case ACCESS_LOCATION:
  278. if (hasAllPermissionGranted(grantResults)) {
  279. Log.i(TAG, "onRequestPermissionsResult: 用户允许权限");
  280. } else {
  281. Log.i(TAG, "onRequestPermissionsResult: 拒绝搜索设备权限");
  282. }
  283. break;
  284. }
  285. }
  286. private boolean hasAllPermissionGranted(int[] grantResults) {
  287. for (int grantResult : grantResults) {
  288. if (grantResult == PackageManager.PERMISSION_DENIED) {
  289. return false;
  290. }
  291. }
  292. return true;
  293. }
  294. // 搜索设备 /
  295. private void searchBtDevice() {
  296. if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
  297. // TODO: Consider calling
  298. // ActivityCompat#requestPermissions
  299. // here to request the missing permissions, and then overriding
  300. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  301. // int[] grantResults)
  302. // to handle the case where the user grants the permission. See the documentation
  303. // for ActivityCompat#requestPermissions for more details.
  304. return;
  305. }
  306. if (bluetoothAdapter.isDiscovering()) { //当前正在搜索设备...
  307. return;
  308. }
  309. //开始搜索
  310. bluetoothAdapter.startDiscovery();
  311. }
  312. // 配对/接触配对设备
  313. /**
  314. * 执行绑定 反射
  315. * @param bluetoothDevice 蓝牙设备
  316. * @return true 执行绑定 false 未执行绑定
  317. */
  318. public boolean boundDevice(BluetoothDevice bluetoothDevice) {
  319. if (bluetoothDevice == null) {
  320. Log.e(TAG, "boundDevice-->bluetoothDevice == null");
  321. return false;
  322. }
  323. try {
  324. return ClsUtils.createBond(BluetoothDevice.class, bluetoothDevice);
  325. } catch (Exception e) {
  326. e.printStackTrace();
  327. }
  328. return true;
  329. }
  330. /**
  331. * 执行解绑 反射
  332. * @param bluetoothDevice 蓝牙设备
  333. * @return true 执行解绑 false未执行解绑
  334. */
  335. public boolean disBoundDevice(BluetoothDevice bluetoothDevice) {
  336. if (bluetoothDevice == null) {
  337. Log.e(TAG, "disBoundDevice-->bluetoothDevice == null");
  338. return false;
  339. }
  340. try {
  341. return ClsUtils.removeBond(BluetoothDevice.class, bluetoothDevice);
  342. } catch (Exception e) {
  343. e.printStackTrace();
  344. }
  345. return true;
  346. }
  347. // 连接设备 ///
  348. /**
  349. * 开始连接设备
  350. * @param bluetoothDevice 蓝牙设备
  351. * @param uuid 发起连接的UUID
  352. * @param conOutTime 连接超时时间
  353. */
  354. public void startConnectDevice(final BluetoothDevice bluetoothDevice, String uuid, long conOutTime) {
  355. if (bluetoothDevice == null) {
  356. Log.e(TAG, "startConnectDevice-->bluetoothDevice == null");
  357. return;
  358. }
  359. if (bluetoothAdapter == null) {
  360. Log.e(TAG, "startConnectDevice-->bluetooth3Adapter == null");
  361. return;
  362. }
  363. //发起连接
  364. connectThread = new ConnectThread(bluetoothAdapter, curBluetoothDevice, uuid);
  365. connectThread.setOnBluetoothConnectListener(new ConnectThread.OnBluetoothConnectListener() {
  366. @Override
  367. public void onStartConn() {
  368. if (ActivityCompat.checkSelfPermission(bluet.this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
  369. // TODO: Consider calling
  370. // ActivityCompat#requestPermissions
  371. // here to request the missing permissions, and then overriding
  372. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  373. // int[] grantResults)
  374. // to handle the case where the user grants the permission. See the documentation
  375. // for ActivityCompat#requestPermissions for more details.
  376. return;
  377. }
  378. Log.d(TAG, "startConnectDevice-->开始连接..." + bluetoothDevice.getName() + "-->" + bluetoothDevice.getAddress());
  379. }
  380. @Override
  381. public void onConnSuccess(BluetoothSocket bluetoothSocket) {
  382. //移除连接超时
  383. mHandler.removeCallbacks(connectOuttimeRunnable);
  384. Log.d(TAG, "startConnectDevice-->移除连接超时");
  385. Log.w(TAG, "startConnectDevice-->连接成功");
  386. Message message = new Message();
  387. message.what = CONNECT_SUCCESS;
  388. mHandler.sendMessage(message);
  389. //标记当前连接状态为true
  390. curConnState = true;
  391. //管理连接,收发数据
  392. managerConnectSendReceiveData(bluetoothSocket);
  393. }
  394. @Override
  395. public void onConnFailure(String errorMsg) {
  396. Log.e(TAG, "startConnectDevice-->" + errorMsg);
  397. Message message = new Message();
  398. message.what = CONNECT_FAILURE;
  399. mHandler.sendMessage(message);
  400. //标记当前连接状态为false
  401. curConnState = false;
  402. //断开管理连接
  403. clearConnectedThread();
  404. }
  405. });
  406. connectThread.start();
  407. //设置连接超时时间
  408. mHandler.postDelayed(connectOuttimeRunnable, conOutTime);
  409. }
  410. //连接超时
  411. private Runnable connectOuttimeRunnable = new Runnable() {
  412. @Override
  413. public void run() {
  414. Log.e(TAG, "startConnectDevice-->连接超时");
  415. Message message = new Message();
  416. message.what = CONNECT_FAILURE;
  417. mHandler.sendMessage(message);
  418. //标记当前连接状态为false
  419. curConnState = false;
  420. //断开管理连接
  421. clearConnectedThread();
  422. }
  423. };
  424. // 断开连接 //
  425. /**
  426. * 断开已有的连接
  427. */
  428. public void clearConnectedThread() {
  429. Log.d(TAG, "clearConnectedThread-->即将断开");
  430. //connectedThread断开已有连接
  431. if (connectedThread == null) {
  432. Log.e(TAG, "clearConnectedThread-->connectedThread == null");
  433. return;
  434. }
  435. connectedThread.terminalClose(connectThread);
  436. //等待线程运行完后再断开
  437. mHandler.postDelayed(new Runnable() {
  438. @Override
  439. public void run() {
  440. connectedThread.cancel(); //释放连接
  441. connectedThread = null;
  442. }
  443. }, 10);
  444. Log.w(TAG, "clearConnectedThread-->成功断开连接");
  445. Message message = new Message();
  446. message.what = DISCONNECT_SUCCESS;
  447. mHandler.sendMessage(message);
  448. }
  449. ///
  450. /// 管理已有连接、收发数据 //
  451. /**
  452. * 管理已建立的连接,收发数据
  453. * @param bluetoothSocket 已建立的连接
  454. */
  455. public void managerConnectSendReceiveData(BluetoothSocket bluetoothSocket) {
  456. //管理已有连接
  457. connectedThread = new ConnectedThread(bluetoothSocket);
  458. connectedThread.start();
  459. connectedThread.setOnSendReceiveDataListener(new ConnectedThread.OnSendReceiveDataListener() {
  460. @Override
  461. public void onSendDataSuccess(byte[] data) {
  462. Log.w(TAG, "发送数据成功,长度" + data.length + "->" + bytes2HexString(data, data.length));
  463. Message message = new Message();
  464. message.what = SEND_SUCCESS;
  465. message.obj = "发送数据成功,长度" + data.length + "->" + bytes2HexString(data, data.length);
  466. mHandler.sendMessage(message);
  467. }
  468. @Override
  469. public void onSendDataError(byte[] data, String errorMsg) {
  470. Log.e(TAG, "发送数据出错,长度" + data.length + "->" + bytes2HexString(data, data.length));
  471. Message message = new Message();
  472. message.what = SEND_FAILURE;
  473. message.obj = "发送数据出错,长度" + data.length + "->" + bytes2HexString(data, data.length);
  474. mHandler.sendMessage(message);
  475. }
  476. @Override
  477. public void onReceiveDataSuccess(byte[] buffer) {
  478. Log.w(TAG, "成功接收数据,长度" + buffer.length + "->" + bytes2HexString(buffer, buffer.length));
  479. Message message = new Message();
  480. message.what = RECEIVE_SUCCESS;
  481. message.obj = "成功接收数据,长度" + buffer.length + "->" + bytes2HexString(buffer, buffer.length);
  482. mHandler.sendMessage(message);
  483. }
  484. @Override
  485. public void onReceiveDataError(String errorMsg) {
  486. Log.e(TAG, "接收数据出错:" + errorMsg);
  487. Message message = new Message();
  488. message.what = RECEIVE_FAILURE;
  489. message.obj = "接收数据出错:" + errorMsg;
  490. mHandler.sendMessage(message);
  491. }
  492. });
  493. }
  494. / 发送数据 /
  495. /**
  496. * 发送数据
  497. * @param data 要发送的数据 字符串
  498. * @param isHex 是否是16进制字符串
  499. * @return true 发送成功 false 发送失败
  500. */
  501. public boolean sendData(String data, boolean isHex) {
  502. if (connectedThread == null) {
  503. Log.e(TAG, "sendData:string -->connectedThread == null");
  504. return false;
  505. }
  506. if (data == null || data.length() == 0) {
  507. Log.e(TAG, "sendData:string-->要发送的数据为空");
  508. return false;
  509. }
  510. if (isHex) { //是16进制字符串
  511. data.replace(" ", ""); //取消空格
  512. //检查16进制数据是否合法
  513. if (data.length() % 2 != 0) {
  514. //不合法,最后一位自动填充0
  515. String lasts = "0" + data.charAt(data.length() - 1);
  516. data = data.substring(0, data.length() - 2) + lasts;
  517. }
  518. Log.d(TAG, "sendData:string -->准备写入:" + data); //加空格显示
  519. return connectedThread.write(hexString2Bytes(data));
  520. }
  521. //普通字符串
  522. Log.d(TAG, "sendData:string -->准备写入:" + data);
  523. return connectedThread.write(data.getBytes());
  524. }
  525. //
  526. /// 数据类型转换 //
  527. /**
  528. * 字节数组-->16进制字符串
  529. * @param b 字节数组
  530. * @param length 字节数组长度
  531. * @return 16进制字符串 有空格类似“0A D5 CD 8F BD E5 F8”
  532. */
  533. public static String bytes2HexString(byte[] b, int length) {
  534. StringBuffer result = new StringBuffer();
  535. String hex;
  536. for (int i = 0; i < length; i++) {
  537. hex = Integer.toHexString(b[i] & 0xFF);
  538. if (hex.length() == 1) {
  539. hex = '0' + hex;
  540. }
  541. result.append(hex.toUpperCase()).append(" ");
  542. }
  543. return result.toString();
  544. }
  545. /**
  546. * hexString2Bytes
  547. * 16进制字符串-->字节数组
  548. * @param src 16进制字符串
  549. * @return 字节数组
  550. */
  551. public static byte[] hexString2Bytes(String src) {
  552. int l = src.length() / 2;
  553. byte[] ret = new byte[l];
  554. for (int i = 0; i < l; i++) {
  555. ret[i] = (byte) Integer
  556. .valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
  557. }
  558. return ret;
  559. }
  560. ///
  561. /**
  562. * 初始化蓝牙
  563. */
  564. private void initBluetooth() {
  565. bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  566. if (bluetoothAdapter == null) {
  567. Toast.makeText(this, "当前手机设备不支持蓝牙", Toast.LENGTH_SHORT).show();
  568. } else {
  569. //手机设备支持蓝牙,判断蓝牙是否已开启
  570. if (bluetoothAdapter.isEnabled()) {
  571. Toast.makeText(this, "手机蓝牙已开启", Toast.LENGTH_SHORT).show();
  572. } else {
  573. //蓝牙没有打开,去打开蓝牙。推荐使用第二种打开蓝牙方式
  574. //第一种方式:直接打开手机蓝牙,没有任何提示
  575. // bluetoothAdapter.enable(); //BLUETOOTH_ADMIN权限
  576. //第二种方式:友好提示用户打开蓝牙
  577. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  578. if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
  579. // TODO: Consider calling
  580. // ActivityCompat#requestPermissions
  581. // here to request the missing permissions, and then overriding
  582. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  583. // int[] grantResults)
  584. // to handle the case where the user grants the permission. See the documentation
  585. // for ActivityCompat#requestPermissions for more details.
  586. return;
  587. }
  588. startActivity(enableBtIntent);
  589. }
  590. }
  591. }
  592. /**
  593. * 初始化蓝牙广播
  594. */
  595. private void initBtBroadcast() {
  596. //注册广播接收
  597. btBroadcastReceiver = new BtBroadcastReceiver();
  598. IntentFilter intentFilter = new IntentFilter();
  599. intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); //开始扫描
  600. intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//扫描结束
  601. intentFilter.addAction(BluetoothDevice.ACTION_FOUND);//搜索到设备
  602. intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); //配对状态监听
  603. registerReceiver(btBroadcastReceiver, intentFilter);
  604. }
  605. @Override
  606. protected void onDestroy() {
  607. super.onDestroy();
  608. //注销广播接收
  609. unregisterReceiver(btBroadcastReceiver);
  610. }
  611. /**
  612. * 蓝牙广播接收器
  613. */
  614. private class BtBroadcastReceiver extends BroadcastReceiver {
  615. @Override
  616. public void onReceive(Context context, Intent intent) {
  617. String action = intent.getAction();
  618. if (TextUtils.equals(action, BluetoothAdapter.ACTION_DISCOVERY_STARTED)) { //开启搜索
  619. Log.d(TAG, "开启搜索...");
  620. Message message = new Message();
  621. message.what = START_DISCOVERY;
  622. mHandler.sendMessage(message);
  623. } else if (TextUtils.equals(action, BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {//完成搜素
  624. Log.d(TAG, "停止搜索...");
  625. Message message = new Message();
  626. message.what = STOP_DISCOVERY;
  627. mHandler.sendMessage(message);
  628. } else if (TextUtils.equals(action, BluetoothDevice.ACTION_FOUND)) { //3.0搜索到设备
  629. //蓝牙设备
  630. BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  631. //信号强度
  632. int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
  633. if (ActivityCompat.checkSelfPermission(bluet.this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
  634. // TODO: Consider calling
  635. // ActivityCompat#requestPermissions
  636. // here to request the missing permissions, and then overriding
  637. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  638. // int[] grantResults)
  639. // to handle the case where the user grants the permission. See the documentation
  640. // for ActivityCompat#requestPermissions for more details.
  641. return;
  642. }
  643. Log.w(TAG, "扫描到设备:" + bluetoothDevice.getName() + "-->" + bluetoothDevice.getAddress());
  644. Message message = new Message();
  645. message.what = DISCOVERY_DEVICE;
  646. message.obj = bluetoothDevice;
  647. mHandler.sendMessage(message);
  648. } else if (TextUtils.equals(action, BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
  649. BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  650. if (ActivityCompat.checkSelfPermission(bluet.this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
  651. // TODO: Consider calling
  652. // ActivityCompat#requestPermissions
  653. // here to request the missing permissions, and then overriding
  654. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  655. // int[] grantResults)
  656. // to handle the case where the user grants the permission. See the documentation
  657. // for ActivityCompat#requestPermissions for more details.
  658. return;
  659. }
  660. int bondSate = bluetoothDevice.getBondState();
  661. switch(bondSate) {
  662. case BluetoothDevice.BOND_NONE:
  663. Log.d(TAG, "已解除配对");
  664. Message message1 = new Message();
  665. message1.what = DEVICE_BOND_NONE;
  666. mHandler.sendMessage(message1);
  667. break;
  668. case BluetoothDevice.BOND_BONDING:
  669. Log.d(TAG, "正在配对...");
  670. Message message2 = new Message();
  671. message2.what = DEVICE_BONDING;
  672. mHandler.sendMessage(message2);
  673. break;
  674. case BluetoothDevice.BOND_BONDED:
  675. Log.d(TAG, "已配对");
  676. Message message3 = new Message();
  677. message3.what = DEVICE_BONDED;
  678. mHandler.sendMessage(message3);
  679. break;
  680. }
  681. }
  682. }
  683. }
  684. }

ConnectedThread.java

ConnectThread.java

ClsUtils.java

太长了,自己去原文下载项目复制粘贴吧,这几个类没什么要修改的,把标红的地方按系统提示给改了就好了。注意复制项目的时候把values文件夹下面的东西也复制一下

并且这个绿色的东西AndroidManifest.xml里面也要声明activity,不然运行不了。


界面

bluetooth.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity"
  9. android:padding="10dp"
  10. android:focusableInTouchMode="true">
  11. <RelativeLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content">
  14. <LinearLayout
  15. android:id="@+id/ll_search"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:orientation="vertical">
  19. <Button
  20. android:id="@+id/bt_search"
  21. android:layout_width="wrap_content"
  22. android:layout_height="48dp"
  23. android:background="@drawable/bt_bg"
  24. android:text="搜索"
  25. android:textColor="#FFFFFF"
  26. android:textSize="14sp" />
  27. <TextView
  28. android:id="@+id/tv_cur_con_state"
  29. android:layout_marginTop="10dp"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:textSize="14sp"
  33. android:textColor="@color/colorPrimary"/>
  34. <TextView
  35. android:id="@+id/tv_cur_bond_state"
  36. android:layout_marginTop="10dp"
  37. android:layout_width="match_parent"
  38. android:layout_height="wrap_content"
  39. android:textSize="14sp"
  40. android:textColor="@color/colorPrimary"/>
  41. </LinearLayout>
  42. <LinearLayout
  43. android:layout_toRightOf="@+id/ll_search"
  44. android:layout_marginLeft="10dp"
  45. android:layout_width="match_parent"
  46. android:layout_height="wrap_content"
  47. android:orientation="vertical">
  48. <TextView
  49. android:id="@+id/tv_name"
  50. android:layout_width="match_parent"
  51. android:layout_height="wrap_content"
  52. android:textSize="14sp"
  53. android:hint="设备名"
  54. android:textColor="@color/colorPrimary"
  55. android:paddingTop="5dp"
  56. android:paddingBottom="5dp"
  57. android:paddingLeft="10dp"
  58. android:background="@drawable/tv_bg"/>
  59. <TextView
  60. android:id="@+id/tv_address"
  61. android:layout_marginTop="10dp"
  62. android:layout_width="match_parent"
  63. android:layout_height="wrap_content"
  64. android:textSize="14sp"
  65. android:hint="设备地址"
  66. android:textColor="@color/colorPrimary"
  67. android:paddingTop="5dp"
  68. android:paddingBottom="5dp"
  69. android:paddingLeft="10dp"
  70. android:background="@drawable/tv_bg"/>
  71. <LinearLayout
  72. android:layout_marginTop="10dp"
  73. android:layout_width="match_parent"
  74. android:layout_height="wrap_content"
  75. android:orientation="horizontal">
  76. <Button
  77. android:id="@+id/bt_connect"
  78. android:layout_width="wrap_content"
  79. android:layout_height="40dp"
  80. android:background="@drawable/bt_bg"
  81. android:text="连接"
  82. android:textColor="#FFFFFF"
  83. android:textSize="14sp"
  84. tools:ignore="TouchTargetSizeCheck" />
  85. <Button
  86. android:id="@+id/bt_disconnect"
  87. android:layout_width="wrap_content"
  88. android:layout_height="40dp"
  89. android:layout_marginLeft="10dp"
  90. android:background="@drawable/bt_bg"
  91. android:text="断开"
  92. android:textColor="#FFFFFF"
  93. android:textSize="14sp"
  94. tools:ignore="TouchTargetSizeCheck" />
  95. </LinearLayout>
  96. <LinearLayout
  97. android:layout_marginTop="10dp"
  98. android:layout_width="match_parent"
  99. android:layout_height="wrap_content"
  100. android:orientation="horizontal">
  101. <Button
  102. android:id="@+id/bt_bound"
  103. android:layout_width="wrap_content"
  104. android:layout_height="40dp"
  105. android:background="@drawable/bt_bg"
  106. android:text="配对"
  107. android:textColor="#FFFFFF"
  108. android:textSize="14sp"
  109. tools:ignore="TouchTargetSizeCheck" />
  110. <Button
  111. android:id="@+id/bt_disBound"
  112. android:layout_width="wrap_content"
  113. android:layout_height="40dp"
  114. android:layout_marginLeft="10dp"
  115. android:background="@drawable/bt_bg"
  116. android:text="解除配对"
  117. android:textColor="#FFFFFF"
  118. android:textSize="14sp"
  119. tools:ignore="TouchTargetSizeCheck" />
  120. </LinearLayout>
  121. </LinearLayout>
  122. </RelativeLayout>
  123. <!--搜索设备列表-->
  124. <LinearLayout
  125. android:id="@+id/ll_device_list"
  126. android:layout_width="match_parent"
  127. android:layout_height="match_parent"
  128. android:padding="5dp"
  129. android:visibility="visible">
  130. <ListView
  131. android:id="@+id/lv_devices"
  132. android:layout_width="match_parent"
  133. android:layout_height="match_parent">
  134. </ListView>
  135. </LinearLayout>
  136. <!--数据收发-->
  137. <LinearLayout
  138. android:id="@+id/ll_data_send_receive"
  139. android:visibility="gone"
  140. android:layout_width="match_parent"
  141. android:layout_height="match_parent"
  142. android:orientation="vertical">
  143. <TextView
  144. android:layout_marginTop="10dp"
  145. android:layout_width="match_parent"
  146. android:layout_height="wrap_content"
  147. android:textSize="14sp"
  148. android:text="设置要发送的数据(十六进制字符串形式)"
  149. android:textColor="@color/colorPrimary"/>
  150. <EditText
  151. android:id="@+id/et_send_msg"
  152. android:layout_marginTop="10dp"
  153. android:layout_width="match_parent"
  154. android:layout_height="wrap_content"
  155. android:textSize="14sp"
  156. android:textColor="#333333"
  157. android:padding="10dp"
  158. android:hint="在此输入要发送的数据"
  159. android:background="@drawable/tv_bg"/>
  160. <Button
  161. android:id="@+id/bt_to_send"
  162. android:layout_marginLeft="10dp"
  163. android:layout_marginTop="10dp"
  164. android:layout_width="wrap_content"
  165. android:layout_height="40dp"
  166. android:text="发送"
  167. android:textColor="#FFFFFF"
  168. android:textSize="14sp"
  169. android:background="@drawable/bt_bg"/>
  170. <TextView
  171. android:layout_marginTop="10dp"
  172. android:layout_width="match_parent"
  173. android:layout_height="wrap_content"
  174. android:textSize="14sp"
  175. android:text="显示接收的数据操作结果(发送成功/发送失败)"
  176. android:textColor="@color/colorPrimary"/>
  177. <ScrollView
  178. android:layout_marginTop="10dp"
  179. android:layout_width="match_parent"
  180. android:layout_height="100dp"
  181. android:background="@drawable/tv_bg">
  182. <TextView
  183. android:id="@+id/tv_send_result"
  184. android:layout_width="match_parent"
  185. android:layout_height="match_parent"
  186. android:padding="10dp"
  187. android:textSize="14sp"
  188. android:hint="此处显示发送数据操作结果"
  189. android:textColor="@color/colorPrimary"/>
  190. </ScrollView>
  191. <TextView
  192. android:layout_marginTop="10dp"
  193. android:layout_width="match_parent"
  194. android:layout_height="wrap_content"
  195. android:textSize="14sp"
  196. android:text="显示接收的数据(以十六进制字符串显示)"
  197. android:textColor="@color/colorPrimary"/>
  198. <ScrollView
  199. android:layout_marginTop="10dp"
  200. android:layout_width="match_parent"
  201. android:layout_height="100dp"
  202. android:background="@drawable/tv_bg">
  203. <TextView
  204. android:id="@+id/tv_receive_result"
  205. android:layout_width="match_parent"
  206. android:layout_height="match_parent"
  207. android:padding="10dp"
  208. android:textSize="14sp"
  209. android:hint="此处显示发送数据操作结果"
  210. android:textColor="@color/colorPrimary"/>
  211. </ScrollView>
  212. </LinearLayout>
  213. </LinearLayout>

device_list.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:orientation="vertical">
  7. <TextView
  8. android:id="@+id/tv_device_name"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:padding="5dp"
  12. android:textColor="@color/colorPrimary"
  13. android:textSize="16sp"/>
  14. <TextView
  15. android:id="@+id/tv_device_address"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:padding="5dp"
  19. android:textColor="@color/colorPrimary"
  20. android:textSize="16sp"/>
  21. </LinearLayout>

权限声明

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools">
  4. <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
  5. <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
  6. <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
  7. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  8. <uses-permission android:name="android.permission.BLUETOOTH" />
  9. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- ACCESS_FINE_LOCATION:允许一个程序访问精确位置(如GPS) -->
  10. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><!-- ACCESS_COARSE_LOCATION:允许一个程序访问CellID或WiFi热点来获取大致的位置 -->
  11. <application
  12. android:allowBackup="true"
  13. android:dataExtractionRules="@xml/data_extraction_rules"
  14. android:fullBackupContent="@xml/backup_rules"
  15. android:icon="@mipmap/ic_launcher"
  16. android:label="@string/app_name"
  17. android:roundIcon="@mipmap/ic_launcher_round"
  18. android:supportsRtl="true"
  19. android:theme="@style/Theme.Bluetooth"
  20. tools:targetApi="31">
  21. <activity
  22. android:name=".bluet"
  23. android:exported="true"
  24. android:label="@string/app_name"
  25. android:theme="@style/AppTheme">
  26. <intent-filter>
  27. <action android:name="android.intent.action.MAIN" />
  28. <category android:name="android.intent.category.LAUNCHER" />
  29. </intent-filter>
  30. </activity>
  31. </application>
  32. </manifest>

新的安卓开发软件在用蓝牙时不仅要声明蓝牙权限,还要声明位置权限。


 运行效果 

搜索后点击配对

配对成功

解除配对成功

连接成功

发送数据成功

说明:先点击配对,首次配对是会弹窗的;再点击连接出现发送数据页面,输入一串数字,显示发送成功,但是接收数据失败,这是出现的问题,我无法解决。并且我尝试了多台设备,只有这个手机设备能够连接成功,还有一个设备是我的耳机,不知道怎么解释这个问题,可能协议不一样?不明白,我还有很多需要学习的地方,这次只是学习别人的代码,演示视频可以看b站,我还有一个专栏介绍了一下如何在拿到别人的项目文件时把项目文件的代码复制到自己的项目中,期间出现了很多问题,最后解决了。记得给手机开权限!还有声明权限!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/956154
推荐阅读
相关标签
  

闽ICP备14008679号