Android-BluetoothSPPLibrary-master

所属分类:单片机开发
开发工具:Java
文件大小:1205KB
下载次数:1
上传日期:2017-12-11 13:19:54
上 传 者李华2222
说明:  蓝牙的功能类,可以创建连接 关闭连接等,也可以读取和测试蓝牙连接
(The functional class of Bluetooth)

文件列表:
.gradle (0, 2015-05-24)
.gradle\2.1 (0, 2015-05-24)
.gradle\2.1\taskArtifacts (0, 2015-05-24)
.gradle\2.1\taskArtifacts\cache.properties (30, 2015-05-24)
.gradle\2.1\taskArtifacts\cache.properties.lock (17, 2015-05-24)
.gradle\2.1\taskArtifacts\fileHashes.bin (218964, 2015-05-24)
.gradle\2.1\taskArtifacts\fileSnapshots.bin (2424702, 2015-05-24)
.gradle\2.1\taskArtifacts\outputFileStates.bin (21662, 2015-05-24)
.gradle\2.1\taskArtifacts\taskArtifacts.bin (74029, 2015-05-24)
.gradle\2.2.1 (0, 2015-05-24)
.gradle\2.2.1\taskArtifacts (0, 2015-05-24)
.gradle\2.2.1\taskArtifacts\cache.properties (30, 2015-05-24)
.gradle\2.2.1\taskArtifacts\cache.properties.lock (17, 2015-05-24)
.gradle\2.2.1\taskArtifacts\fileHashes.bin (92712, 2015-05-24)
.gradle\2.2.1\taskArtifacts\fileSnapshots.bin (447642, 2015-05-24)
.gradle\2.2.1\taskArtifacts\outputFileStates.bin (21482, 2015-05-24)
.gradle\2.2.1\taskArtifacts\taskArtifacts.bin (109290, 2015-05-24)
.idea (0, 2015-05-24)
.idea\.name (12, 2015-05-24)
.idea\compiler.xml (711, 2015-05-24)
.idea\copyright (0, 2015-05-24)
.idea\copyright\profiles_settings.xml (74, 2015-05-24)
.idea\encodings.xml (166, 2015-05-24)
.idea\gradle.xml (615, 2015-05-24)
.idea\misc.xml (396, 2015-05-24)
.idea\modules.xml (613, 2015-05-24)
.idea\scopes (0, 2015-05-24)
.idea\scopes\scope_settings.xml (139, 2015-05-24)
.idea\vcs.xml (218, 2015-05-24)
.idea\workspace.xml (138821, 2015-05-24)
.travis.yml (179, 2015-05-24)
LICENSE.txt (10833, 2015-05-24)
app (0, 2015-05-24)
app\build.gradle (809, 2015-05-24)
app\proguard-rules.pro (670, 2015-05-24)
... ...

[![Build Status](https://travis-ci.org/akexorcist/Android-BluetoothSPP.svg?branch=master)](https://travis-ci.org/akexorcist/Android-BluetoothSPP) Android-BluetoothSPPLibrary =========================== ![BluetoothSPP Library](https://raw.githubusercontent.com/akexorcist/Android-BluetoothSPPLibrary/master/image/header.png) Bluetooth Serial Port Profile which comfortable to developer application to communication with microcontroller or android device via bluetooth. This libraly include all important methods for serial port profile on bluetooth communication. It has built-in bluetooth device list. Feature -------------- It's very easy to use Solve the lack of data like as "abcdefg" which divided to "abc" and "defg" when receive these data Auto add LF (0x0A) and CR (0x0D) when send data to connection device No need to create layout for bluetooth device list to select device for connection. You can use built-in layout in this library and you can customize layout if you want Auto connection supported Listener for receive data from connection device Download -------------- Maven ``` com.akexorcist bluetoothspp 1.0.0 ``` Gradle ``` compile 'com.akexorcist:bluetoothspp:1.0.0' ``` Simple Usage -------------- Import this library to your workspace and include in to your android project For Eclipse ADT : Download this library and import into your workspace and include this library to your project For Android Studio : Use Gradle to download this library from Maven Declare permission for library ```xml ``` Declare BluetoothSPP like this ```java BluetoothSPP bt = new BluetoothSPP(Context); ``` Check if bluetooth is now available ```java if(!bt.isBluetoothAvailable()) { // any command for bluetooth is not available } ``` Check if bluetooth is not enable when activity is onStart ```java public void onStart() { super.onStart(); if(!bt.isBluetoothEnable()) { // Do somthing if bluetooth is disable } else { // Do something if bluetooth is already enable } } ``` if bluetooth is ready call this method to start service For connection with android device ```java bt.startService(BluetoothState.DEVICE_ANDROID); ``` ![Communicate with android](https://raw.githubusercontent.com/akexorcist/Android-BluetoothSPPLibrary/master/image/Connection.png) For connection with any microcontroller which communication with bluetooth serial port profile module ```java bt.startService(BluetoothState.DEVICE_OTHER); ``` ![Communicate with microcontroller](https://raw.githubusercontent.com/akexorcist/Android-BluetoothSPPLibrary/master/image/Connection2.png) ![Bluetooth module with SPP](https://raw.githubusercontent.com/akexorcist/Android-BluetoothSPPLibrary/master/image/BlueStick.png) Stop service with ```java bt.stopService(); ``` Intent to choose device activity ```java Intent intent = new Intent(getApplicationContext(), DeviceList.class); startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE); ``` don't forget declare library activty like this ```java ``` After intent to choose device activity and finish that activity. You need to check result data on onActivityResult ```java public void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) { if(resultCode == Activity.RESULT_OK) bt.connect(data); } else if(requestCode == BluetoothState.REQUEST_ENABLE_BT) { if(resultCode == Activity.RESULT_OK) { bt.setupService(); bt.startService(BluetoothState.DEVICE_ANDROID); setup(); } else { // Do something if user doesn't choose any device (Pressed back) } } } ``` If you want to send any data. boolean parameter is mean that data will send with ending by LF and CR or not. If yes your data will added by LF & CR ```java bt.send("Message", true); ``` or ```java bt.send(new byte[] { 0x30, 0x38, ....}, false); ``` Listener for data receiving ```java bt.setOnDataReceivedListener(new OnDataReceivedListener() { public void onDataReceived(byte[] data, String message) { // Do something when data incoming } }); ``` Listener for bluetooth connection atatus ```java bt.setBluetoothConnectionListener(new BluetoothConnectionListener() { public void onDeviceConnected(String name, String address) { // Do something when successfully connected } public void onDeviceDisconnected() { // Do something when connection was disconnected } public void onDeviceConnectionFailed() { // Do something when connection failed } }); ``` Listener when bluetooth connection has changed ```java bt.setBluetoothStateListener(new BluetoothStateListener() { public void onServiceStateChanged(int state) { if(state == BluetoothState.STATE_CONNECTED) // Do something when successfully connected else if(state == BluetoothState.STATE_CONNECTING) // Do something while connecting else if(state == BluetoothState.STATE_LISTEN) // Do something when device is waiting for connection else if(state == BluetoothState.STATE_NONE) // Do something when device don't have any connection } }); ``` Using auto connection ```java bt.autoConnect("Keyword for filter paired device"); ``` Listener for auto connection ```java bt.setAutoConnectionListener(new AutoConnectionListener() { public void onNewConnection(String name, String address) { // Do something when earching for new connection device } public void onAutoConnectionStarted() { // Do something when auto connection has started } }); ``` Customize device list's layout by create layout which include list view with id name = "list_devices" button with id name = "button_scan" *Example* ```xml

近期下载者

相关文件


收藏者