Android 蓝牙通信——AndroidBluetoothManager
Johnny03Y
8年前
<p> </p> <p>To get a Git project into your build:</p> <p>Step 1. Add the JitPack repository to your build file</p> <p>Add it in your root build.gradle at the end of repositories:</p> <pre> <code class="language-java">allprojects { repositories { ... maven { url 'https://jitpack.io' } } }</code></pre> <p>Step 2. Add the dependency</p> <pre> <code class="language-java">dependencies { compile 'com.github.kongqw:AndroidBluetoothManager:1.0.0' }</code></pre> <p><a href="/misc/goto?guid=4959742070885647974" rel="nofollow,noindex">AndroidBluetoothManager</a></p> <h2>效果图</h2> <p style="text-align:center"><img src="https://simg.open-open.com/show/581f8dcf4b4940f22b914abea0b8c0ba.png"></p> <p><a href="/misc/goto?guid=4959742070980070516" rel="nofollow,noindex">PNG</a></p> <p><a href="/misc/goto?guid=4959742071060796864" rel="nofollow,noindex">GIF</a></p> <h2>基础功能</h2> <h2>添加权限</h2> <pre> <code class="language-java"><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" /></code></pre> <h2>初始化</h2> <pre> <code class="language-java">mBluetoothManager = new BluetoothManager();</code></pre> <h2>打开蓝牙</h2> <pre> <code class="language-java">mBluetoothManager.openBluetooth();</code></pre> <h2>关闭蓝牙</h2> <pre> <code class="language-java">mBluetoothManager.closeBluetooth();</code></pre> <h2>添加蓝牙开关状态的监听</h2> <pre> <code class="language-java">mBluetoothManager.setOnBluetoothStateListener(this);</code></pre> <pre> <code class="language-java">/** * 正在关闭蓝牙的回调 */ @Override public void onBluetoothStateTurningOff() { // TODO } /** * 蓝牙关闭的回调 */ @Override public void onBluetoothStateOff() { // TODO } /** * 正在打开蓝牙的回调 */ @Override public void onBluetoothStateTurningOn() { // TODO } /** * 蓝牙打开的回调 */ @Override public void onBluetoothStateOn() { // TODO }</code></pre> <h2>移除蓝牙开关状态的监听</h2> <pre> <code class="language-java">mBluetoothManager.removeOnBluetoothStateListener();</code></pre> <h2>设置蓝牙可见</h2> <pre> <code class="language-java">startActivity(mBluetoothManager.getDurationIntent(0));</code></pre> <h2>获取蓝牙名称</h2> <pre> <code class="language-java">mBluetoothManager.getName()</code></pre> <h2>修改蓝牙名称</h2> <pre> <code class="language-java">mBluetoothManager.setName(newName);</code></pre> <h2>扫描附近的蓝牙设备</h2> <pre> <code class="language-java">mBluetoothManager.discovery();</code></pre> <h2>添加扫描蓝牙设备的监听</h2> <pre> <code class="language-java">mBluetoothManager.setOnDiscoveryDeviceListener(this);</code></pre> <pre> <code class="language-java">/** * 开始扫描附近蓝牙设备的回调 */ @Override public void onDiscoveryDeviceStarted() { // TODO } /** * 扫描到附近蓝牙设备的回调 * * @param device 蓝牙设备 */ @Override public void onDiscoveryDeviceFound(BluetoothDevice device) { // TODO } /** * 扫描附近蓝牙设备完成的回调 */ @Override public void onDiscoveryDeviceFinished() { // TODO }</code></pre> <h2>移除扫描蓝牙设备的监听</h2> <pre> <code class="language-java">mBluetoothManager.removeOnDiscoveryDeviceListener();</code></pre> <h2>服务端</h2> <h2>初始化</h2> <pre> <code class="language-java">mBluetoothService = new BluetoothService() { @Override protected UUID onSecureUuid() { // TODO 设置自己的UUID return UUID_SECURE; } @Override protected UUID onInsecureUuid() { // TODO 设置自己的UUID return UUID_INSECURE; } };</code></pre> <h2>等待客户端连接</h2> <pre> <code class="language-java">mBluetoothService.start();</code></pre> <h2>断开连接/释放资源</h2> <pre> <code class="language-java">mBluetoothService.stop();</code></pre> <h2>添加蓝牙连接的监听</h2> <pre> <code class="language-java">mBluetoothService.setOnServiceConnectListener(new OnServiceConnectListener() { @Override public void onConnectListening() { // TODO } @Override public void onConnectSuccess(BluetoothDevice device) { // TODO } @Override public void onConnectFail(Exception e) { // TODO } @Override public void onConnectLost(Exception e) { // TODO } });</code></pre> <h2>发送消息</h2> <pre> <code class="language-java">mBluetoothService.send(chatText);</code></pre> <h2>添加消息收发的监听</h2> <pre> <code class="language-java">mBluetoothClient.setOnMessageListener(this);</code></pre> <pre> <code class="language-java">/** * 蓝牙发送了消息 * * @param message 发送的消息 */ @Override public void onSend(String message) { // TODO } /** * 蓝牙接收到消息 * * @param message 接收的消息 */ @Override public void onRead(String message) { // TODO }</code></pre> <h2>客户端</h2> <h2>初始化</h2> <pre> <code class="language-java">mBluetoothClient = new BluetoothClient() { @Override protected UUID onSecureUuid() { // TODO 设置自己的UUID return UUID_SECURE; } @Override protected UUID onInsecureUuid() { // TODO 设置自己的UUID return UUID_INSECURE; } };</code></pre> <h2>蓝牙连接(安全)</h2> <pre> <code class="language-java">mBluetoothClient.connect(mBluetoothDevice, true);</code></pre> <h2>蓝牙连接(不安全)</h2> <pre> <code class="language-java">mBluetoothClient.connect(mBluetoothDevice, false);</code></pre> <h2>断开连接/释放资源</h2> <pre> <code class="language-java">mBluetoothClient.stop();</code></pre> <h2>添加蓝牙连接的监听</h2> <pre> <code class="language-java">mBluetoothClient.setOnClientConnectListener(new OnClientConnectListener() { @Override public void onConnecting() { // TODO } @Override public void onConnectSuccess(BluetoothDevice device) { // TODO } @Override public void onConnectFail(Exception e) { // TODO } @Override public void onConnectLost(Exception e) { // TODO } });</code></pre> <h2>发送消息</h2> <pre> <code class="language-java">mBluetoothClient.send(chatText);</code></pre> <h2>添加消息收发的监听</h2> <pre> <code class="language-java">mBluetoothClient.setOnMessageListener(this);</code></pre> <pre> <code class="language-java">/** * 蓝牙发送了消息 * * @param message 发送的消息 */ @Override public void onSend(String message) { // TODO } /** * 蓝牙接收到消息 * * @param message 接收的消息 */ @Override public void onRead(String message) { // TODO }</code></pre> <p> </p> <p> </p> <p> </p>