├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── activity_service.xml
│ │ │ │ └── activity_client.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── xloong
│ │ │ │ └── bluetoothsocketdemo
│ │ │ │ ├── GlideLoader.java
│ │ │ │ ├── MainActivity.java
│ │ │ │ ├── BaseActivity.java
│ │ │ │ ├── ServiceActivity.java
│ │ │ │ └── ClientActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── xloong
│ │ │ └── bluetoothsocketdemo
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── xloong
│ │ └── bluetoothsocketdemo
│ │ └── ApplicationTest.java
├── proguard-rules.pro
└── build.gradle
├── library_bluesocket
├── .gitignore
├── src
│ ├── main
│ │ ├── aidl
│ │ │ ├── android
│ │ │ │ └── bluetooth
│ │ │ │ │ └── BluetoothDevice.aidl
│ │ │ └── com
│ │ │ │ └── xloong
│ │ │ │ └── library
│ │ │ │ └── bluesocket
│ │ │ │ └── IBluetooth.aidl
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── xloong
│ │ │ └── library
│ │ │ └── bluesocket
│ │ │ ├── BlueMessageReceiver.java
│ │ │ ├── BlueClientThread.java
│ │ │ ├── message
│ │ │ ├── IMessage.java
│ │ │ ├── StringMessage.java
│ │ │ └── ImageMessage.java
│ │ │ ├── BlueServiceThread.java
│ │ │ ├── BlueSocketBaseThread.java
│ │ │ ├── utils
│ │ │ └── TypeUtils.java
│ │ │ ├── BlueManager.java
│ │ │ ├── BlueService.java
│ │ │ ├── BlueDataThread.java
│ │ │ └── BluetoothSppHelper.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── xloong
│ │ │ └── library
│ │ │ └── bluesocket
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── xloong
│ │ └── library
│ │ └── bluesocket
│ │ └── ApplicationTest.java
├── build.gradle
└── proguard-rules.pro
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── .github
└── workflows
│ └── gradle.yml
├── gradle.properties
├── README.md
├── gradlew.bat
├── gradlew
└── LICENSE
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library_bluesocket/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library_bluesocket'
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hnlbxb2004/BluetoothSocket/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | BluetoothSocketDemo
3 |
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hnlbxb2004/BluetoothSocket/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hnlbxb2004/BluetoothSocket/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hnlbxb2004/BluetoothSocket/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hnlbxb2004/BluetoothSocket/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hnlbxb2004/BluetoothSocket/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/library_bluesocket/src/main/aidl/android/bluetooth/BluetoothDevice.aidl:
--------------------------------------------------------------------------------
1 | // BluetoothDevice.aidl
2 | package android.bluetooth;
3 | parcelable BluetoothDevice;
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/test/java/com/xloong/bluetoothsocketdemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.xloong.bluetoothsocketdemo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/library_bluesocket/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.3"
6 |
7 | defaultConfig {
8 | minSdkVersion 19
9 | targetSdkVersion 23
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 |
14 | }
15 |
16 | dependencies {
17 | compile fileTree(dir: 'libs', include: ['*.jar'])
18 | testCompile 'junit:junit:4.12'
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/library_bluesocket/src/test/java/com/xloong/library/bluesocket/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/xloong/bluetoothsocketdemo/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.xloong.bluetoothsocketdemo;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/library_bluesocket/src/androidTest/java/com/xloong/library/bluesocket/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/library_bluesocket/src/main/aidl/com/xloong/library/bluesocket/IBluetooth.aidl:
--------------------------------------------------------------------------------
1 | // IBluetooth.aidl
2 | package com.xloong.library.bluesocket;
3 |
4 | import android.bluetooth.BluetoothDevice;
5 |
6 | // Declare any non-default types here with import statements
7 |
8 | interface IBluetooth {
9 | // 蓝牙是否连接
10 | boolean isConnected();
11 |
12 | //启动service 端 startService 和 connectionService只能用一个。
13 | void startService();
14 |
15 | //连接Service 端 startService 和 connectionService只能用一个。
16 | void connectionService(in BluetoothDevice device);
17 |
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/library_bluesocket/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.github/workflows/gradle.yml:
--------------------------------------------------------------------------------
1 | # This workflow will build a Java project with Gradle
2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle
3 |
4 | name: Java CI with Gradle
5 |
6 | on:
7 | push:
8 | branches: [ master ]
9 | pull_request:
10 | branches: [ master ]
11 |
12 | jobs:
13 | build:
14 |
15 | runs-on: ubuntu-latest
16 |
17 | steps:
18 | - uses: actions/checkout@v2
19 | - name: Set up JDK 1.8
20 | uses: actions/setup-java@v1
21 | with:
22 | java-version: 1.8
23 | - name: Grant execute permission for gradlew
24 | run: chmod +x gradlew
25 | - name: Build with Gradle
26 | run: ./gradlew build
27 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xloong/bluetoothsocketdemo/GlideLoader.java:
--------------------------------------------------------------------------------
1 | package com.xloong.bluetoothsocketdemo;
2 |
3 | import android.content.Context;
4 | import android.widget.ImageView;
5 |
6 | import com.bumptech.glide.Glide;
7 | import com.jaiky.imagespickers.ImageLoader;
8 |
9 | /**
10 | * Created by xubingbing on 2017/8/10.
11 | */
12 |
13 | public class GlideLoader implements ImageLoader {
14 |
15 | @Override
16 | public void displayImage(Context context, String path, ImageView imageView) {
17 | Glide.with(context)
18 | .load(path)
19 | .placeholder(com.jaiky.imagespickers.R.drawable.global_img_default)
20 | .centerCrop()
21 | .into(imageView);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/bingbing/Documents/tools/sdk/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xloong/bluetoothsocketdemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.xloong.bluetoothsocketdemo;
2 |
3 | import android.content.Intent;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.os.Bundle;
6 | import android.view.View;
7 |
8 | public class MainActivity extends AppCompatActivity {
9 |
10 | @Override
11 | protected void onCreate(Bundle savedInstanceState) {
12 | super.onCreate(savedInstanceState);
13 | setContentView(R.layout.activity_main);
14 | }
15 |
16 | public void client(View view){
17 | startActivity(new Intent(this, ClientActivity.class));
18 | }
19 |
20 | public void service(View view){
21 | startActivity(new Intent(this,ServiceActivity.class));
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/library_bluesocket/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/bingbing/Documents/tools/sdk/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 24
5 | buildToolsVersion "23.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.xloong.bluetoothsocketdemo"
9 | minSdkVersion 19
10 | targetSdkVersion 23
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | testCompile 'junit:junit:4.12'
25 | compile 'com.android.support:appcompat-v7:23.2.1'
26 | compile 'com.jaikydota.imagespickers:imagespickers:1.0.6'
27 |
28 | //如果使用图片加载框架,添加依赖,下面用Glide示例
29 | compile 'com.github.bumptech.glide:glide:3.6.1'
30 | compile project(':library_bluesocket')
31 | }
32 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_service.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
11 |
12 |
16 |
17 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/library_bluesocket/src/main/java/com/xloong/library/bluesocket/BlueMessageReceiver.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket;
2 |
3 | import android.content.BroadcastReceiver;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.IntentFilter;
7 |
8 | import com.xloong.library.bluesocket.message.IMessage;
9 |
10 | /**
11 | *
12 | * 用来监听消息到达。
13 | * Created by xubingbing on 2017/8/10.
14 | */
15 | public abstract class BlueMessageReceiver extends BroadcastReceiver {
16 |
17 |
18 | public void register(Context context){
19 | IntentFilter intentFilter = new IntentFilter();
20 | intentFilter.addAction(BlueService.ACTION_MESSAGE_REVEIVER);
21 | context.registerReceiver(this,intentFilter);
22 | }
23 |
24 | public void unregister(Context context){
25 | context.unregisterReceiver(this);
26 | }
27 |
28 | @Override
29 | public void onReceive(Context context, Intent intent) {
30 | if (BlueService.ACTION_MESSAGE_REVEIVER.equals(intent.getAction())){
31 | IMessage message = intent.getParcelableExtra("message");
32 | if (message != null){
33 | onMessageReceiver(message);
34 | }
35 | }
36 | }
37 |
38 |
39 | public abstract void onMessageReceiver(IMessage message);
40 |
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_client.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
11 |
16 |
20 |
26 |
27 |
33 |
37 |
38 |
--------------------------------------------------------------------------------
/library_bluesocket/src/main/java/com/xloong/library/bluesocket/BlueClientThread.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket;
2 |
3 | import android.bluetooth.BluetoothDevice;
4 | import android.bluetooth.BluetoothSocket;
5 | import android.os.Handler;
6 |
7 | import java.io.IOException;
8 |
9 | /**
10 | * 蓝牙连接客户端连接线程
11 | * @author bingbing
12 | * @date 16/4/7
13 | */
14 | public class BlueClientThread extends BlueSocketBaseThread {
15 |
16 | private BluetoothDevice mServiceDevice;
17 | private BluetoothSocket mBlueSocket;
18 |
19 | public BlueClientThread(BluetoothDevice serviceDevice, Handler handler) {
20 | super(handler);
21 | mServiceDevice = serviceDevice;
22 | }
23 |
24 |
25 | @Override
26 | public void run() {
27 | super.run();
28 | if (!isRunning)return;
29 | try {
30 | sendMessage(BlueSocketStatus.CONNECTIONING);
31 | mBlueSocket = mServiceDevice.createRfcommSocketToServiceRecord(UUID_ANDROID_DEVICE);
32 | mBlueSocket.connect();
33 | sendMessage(BlueSocketStatus.ACCEPTED);
34 | } catch (IOException e) {
35 | sendMessage(BlueSocketStatus.DISCONNECTION);
36 | }
37 | }
38 |
39 |
40 | @Override
41 | public BluetoothSocket getSocket() {
42 | return mBlueSocket;
43 | }
44 |
45 | @Override
46 | public void cancle() {
47 | super.cancle();
48 |
49 | try {
50 | if (mBlueSocket != null)
51 | mBlueSocket.close();
52 | } catch (IOException e) {
53 | e.printStackTrace();
54 | }
55 | mServiceDevice = null;
56 | mBlueSocket = null;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/library_bluesocket/src/main/java/com/xloong/library/bluesocket/message/IMessage.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket.message;
2 |
3 | import android.os.Parcelable;
4 |
5 | import java.io.IOException;
6 | import java.io.InputStream;
7 | import java.io.OutputStream;
8 |
9 | /**
10 | * 蓝牙消息
11 | * Created by xubingbing on 2017/8/10.
12 | */
13 | public interface IMessage extends Parcelable{
14 | byte TYPE_String = 1;
15 | byte TYPE_BYTE = 2;
16 | byte HEADER = 'H';
17 |
18 |
19 | /**
20 | * 当前消息的类型,是字符串,还是byte
21 | * @return
22 | */
23 | byte getType();
24 |
25 | void setType(byte type);
26 |
27 | /**
28 | * 消息体的内容 长度
29 | * @return
30 | */
31 | long getLength();
32 |
33 | /**
34 | * 设置长度
35 | * @param length
36 | */
37 | void setLength(long length);
38 | /**
39 | * 获取消息内容
40 | * @return
41 | */
42 | T getContent();
43 |
44 |
45 | void setExtend(String extend);
46 |
47 | /**
48 | * 设置Content 内容
49 | * @param content
50 | * @param extend
51 | */
52 | void setContent(T content, String extend);
53 |
54 | /**
55 | * 解析content 内容
56 | */
57 | void parseContent(InputStream inputStream) throws IOException;
58 |
59 | /**
60 | * 将消息写入流
61 | * @param outputStream
62 | */
63 | void writeContent(OutputStream outputStream) throws IOException;
64 |
65 | /**
66 | * 创建消息Header
67 | *
68 | * header 协议,1.前1个字节为魔数,代表Header, 固定 H.getByte()
69 | * 2.内容类型,1个字节,
70 | * 3.长度, 8个字节,
71 | * 4.剩余,扩展消息
72 | * 5.结尾 \r\n
73 | *
74 | * @return
75 | */
76 | byte[] creatHeader();
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # BluetoothSocket
2 |
3 | 此library 库可以方便的帮你使用经典蓝牙做socket 通讯。
4 | 网上的一些蓝牙游戏互联都是用的经典蓝牙做socket ,
5 |
6 |
7 | # 使用
8 | ###service端 启动,等待被连接
9 |
10 |
11 | ```java
12 |
13 | BluetoothSocketHelper mHelper = new BluetoothSocketHelper();
14 |
15 | mHelper.strat();
16 |
17 |
18 | ```
19 | ### Client 端,连接设
20 |
21 | ```java
22 |
23 | BluetoothSocketHelper mHelper = new BluetoothSocketHelper();
24 | mHelper.connect(mBluetoothDevice);
25 |
26 | ```
27 |
28 |
29 | ### service 和 client 发送消息
30 | ```java
31 | mHelper.write("要写入的消息");
32 |
33 | ```
34 |
35 |
36 | ###停止
37 | ```java
38 | mHelper.stop();
39 |
40 | ```
41 |
42 |
43 | ###需要注意
44 | Client 连接前需要确保改设备应配对完成
45 |
46 |
47 | ## License
48 | The MIT License (MIT) Copyright (c) 2016 hnlbxb2004
49 |
50 |
51 |
52 | Permission is hereby granted, free of charge, to any person obtaining a copy
53 | of this software and associated documentation files (the "Software"), to deal
54 | in the Software without restriction, including without limitation the rights
55 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
56 | copies of the Software, and to permit persons to whom the Software is
57 | furnished to do so, subject to the following conditions:
58 |
59 | The above copyright notice and this permission notice shall be included in all
60 | copies or substantial portions of the Software.
61 |
62 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
63 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
64 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
65 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
66 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
67 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
68 | SOFTWARE.
69 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xloong/bluetoothsocketdemo/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package com.xloong.bluetoothsocketdemo;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.util.Log;
6 | import android.view.View;
7 |
8 | import com.jaiky.imagespickers.ImageConfig;
9 | import com.jaiky.imagespickers.ImageSelector;
10 | import com.jaiky.imagespickers.ImageSelectorActivity;
11 |
12 | import java.util.List;
13 |
14 | /**
15 | * Created by xubingbing on 2017/8/10.
16 | */
17 |
18 | public abstract class BaseActivity extends Activity{
19 |
20 |
21 |
22 | public void chooseImage(){
23 | ImageConfig imageConfig
24 | = new ImageConfig.Builder(new GlideLoader())
25 | .steepToolBarColor(getResources().getColor(R.color.blue))
26 | .titleBgColor(getResources().getColor(R.color.blue))
27 | .titleSubmitTextColor(getResources().getColor(R.color.white))
28 | .titleTextColor(getResources().getColor(R.color.white))
29 | // 开启单选 (默认为多选)
30 | .singleSelect()
31 | // 开启拍照功能 (默认关闭)
32 | .showCamera()
33 | // 拍照后存放的图片路径(默认 /temp/picture) (会自动创建)
34 | .filePath("/temp/picture")
35 | .build();
36 |
37 |
38 | ImageSelector.open(this, imageConfig); // 开启图片选择器
39 | }
40 |
41 |
42 | @Override
43 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
44 | super.onActivityResult(requestCode, resultCode, data);
45 | if (requestCode == ImageSelector.IMAGE_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
46 | // 获取选中的图片路径列表 Get Images Path List
47 | List pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
48 |
49 | onChooseImage(pathList.get(0));
50 | for (String path : pathList) {
51 | Log.i("ImagePath", path);
52 | }
53 | }
54 | }
55 |
56 | public abstract void onChooseImage(String path);
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/library_bluesocket/src/main/java/com/xloong/library/bluesocket/BlueServiceThread.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket;
2 |
3 | import android.bluetooth.BluetoothServerSocket;
4 | import android.bluetooth.BluetoothSocket;
5 | import android.os.Handler;
6 |
7 |
8 | import java.io.IOException;
9 |
10 | /**
11 | * Service 端等待被连接的线程
12 | *
13 | * @author bingbing
14 | * @date 16/4/6
15 | */
16 | public class BlueServiceThread extends BlueSocketBaseThread {
17 |
18 |
19 | private BluetoothServerSocket mBlueServiceSocket;
20 | private BluetoothSocket mBlueSocket;
21 |
22 | public BlueServiceThread(Handler handler) {
23 | super(handler);
24 | }
25 |
26 | @Override
27 | public BluetoothSocket getSocket() {
28 | return mBlueSocket;
29 | }
30 |
31 | @Override
32 | public void run() {
33 |
34 | if (!isRunning) return;
35 |
36 | try {
37 | mBlueServiceSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, UUID_ANDROID_DEVICE);
38 | sendMessage(BlueSocketStatus.LISTENING);
39 | //监听连接,等待客户端连接,此处会阻塞线程,如果客户端没有连接服务端,此处一直会等待,知道有设备连接
40 | mBlueSocket = mBlueServiceSocket.accept();
41 | if (mBlueSocket != null) {
42 | sendMessage(BlueSocketStatus.ACCEPTED);
43 | } else {
44 | sendMessage(BlueSocketStatus.DISCONNECTION);
45 | }
46 | } catch (IOException e) {
47 | sendMessage(BlueSocketStatus.DISCONNECTION);
48 | }
49 | }
50 |
51 | @Override
52 | public void cancle() {
53 | super.cancle();
54 | try {
55 | if (mBlueServiceSocket != null)
56 | mBlueServiceSocket.close();
57 | } catch (IOException e) {
58 | e.printStackTrace();
59 | }
60 |
61 | try {
62 | if (mBlueSocket != null)
63 | mBlueSocket.close();
64 | } catch (IOException e) {
65 | e.printStackTrace();
66 | }
67 | mBlueServiceSocket = null;
68 | mBlueSocket = null;
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/library_bluesocket/src/main/java/com/xloong/library/bluesocket/BlueSocketBaseThread.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket;
2 |
3 | import android.bluetooth.BluetoothAdapter;
4 | import android.bluetooth.BluetoothSocket;
5 | import android.os.Handler;
6 |
7 | import java.util.UUID;
8 |
9 | /**
10 | * @author bingbing
11 | * @date 16/4/6
12 | */
13 | public abstract class BlueSocketBaseThread extends Thread {
14 |
15 | protected final String TAG = this.getClass().getSimpleName();
16 | public static final String NAME_SECURE = "XLOONG Bluetooth";
17 |
18 | public static final UUID UUID_ANDROID_DEVICE = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
19 |
20 | public enum BlueSocketStatus {
21 |
22 | NONE, //初始状态
23 | LISTENING, //服务端正在监听被连接状态
24 | ACCEPTED, //服务端接受到连接申请 或者客户端响应到服务端的连接
25 | CONNECTIONING, //正在建立连接通道
26 | CONNEDTIONED, //已经连接
27 | DISCONNECTION, //断开连接
28 | MESSAGERECEIVE //消息达到
29 | }
30 |
31 | /**
32 | * 此handler 用来和 manager 通讯
33 | */
34 | protected Handler mHandler;
35 | /**
36 | * 是否还在运行
37 | */
38 | protected boolean isRunning;
39 |
40 | protected BluetoothAdapter mBluetoothAdapter;
41 |
42 | protected BlueSocketBaseThread(Handler handler) {
43 | super();
44 | mHandler = handler;
45 | mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
46 | }
47 |
48 |
49 | /**
50 | * 获取当前的链接的socket 对象
51 | *
52 | * @return
53 | */
54 | public abstract BluetoothSocket getSocket();
55 |
56 | /**
57 | * 取消当前线程,释放内存
58 | */
59 | public void cancle() {
60 | isRunning = false;
61 | mBluetoothAdapter = null;
62 | }
63 |
64 | public void sendMessage(BlueSocketStatus status) {
65 | if (mHandler != null && isRunning)
66 | mHandler.obtainMessage(status.ordinal()).sendToTarget();
67 | }
68 |
69 | public void sendMessage(BlueSocketStatus status, Object object) {
70 | if (mHandler != null && isRunning)
71 | mHandler.obtainMessage(status.ordinal(), object).sendToTarget();
72 | }
73 |
74 |
75 | @Override
76 | public synchronized void start() {
77 | isRunning = true;
78 | super.start();
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xloong/bluetoothsocketdemo/ServiceActivity.java:
--------------------------------------------------------------------------------
1 | package com.xloong.bluetoothsocketdemo;
2 |
3 | import android.app.Activity;
4 | import android.bluetooth.BluetoothDevice;
5 | import android.os.Bundle;
6 | import android.view.View;
7 | import android.widget.EditText;
8 | import android.widget.TextView;
9 | import android.widget.Toast;
10 |
11 | import com.xloong.library.bluesocket.BlueSocketBaseThread;
12 | import com.xloong.library.bluesocket.BluetoothSppHelper;
13 | import com.xloong.library.bluesocket.message.IMessage;
14 | import com.xloong.library.bluesocket.message.ImageMessage;
15 | import com.xloong.library.bluesocket.message.StringMessage;
16 |
17 | import java.io.File;
18 |
19 | /**
20 | * @author bingbing
21 | * @date 16/4/7
22 | */
23 | public class ServiceActivity extends BaseActivity implements BluetoothSppHelper.BlueSocketListener {
24 |
25 | private EditText mEdit;
26 | private BluetoothSppHelper mHelper;
27 | private TextView mStatus;
28 |
29 | @Override
30 | protected void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | setContentView(R.layout.activity_service);
33 | mEdit = (EditText) findViewById(R.id.edit);
34 | mStatus = (TextView) findViewById(R.id.text);
35 | mHelper = new BluetoothSppHelper();
36 | mHelper.setBlueSocketListener(this);
37 | mHelper.strat();
38 | }
39 |
40 |
41 | @Override
42 | public void onBlueSocketStatusChange(BlueSocketBaseThread.BlueSocketStatus status, BluetoothDevice remoutDevice) {
43 | mStatus.setText(status.toString());
44 | }
45 |
46 | @Override
47 | public void onBlueSocketMessageReceiver(IMessage message) {
48 | if (message instanceof StringMessage){
49 | Toast.makeText(this, ((StringMessage)message).getContent(), Toast.LENGTH_SHORT).show();
50 | }else if (message instanceof ImageMessage){
51 | Toast.makeText(this, ((ImageMessage)message).getContent().getAbsolutePath(), Toast.LENGTH_SHORT).show();
52 | }
53 | }
54 |
55 | @Override
56 | protected void onDestroy() {
57 | super.onDestroy();
58 | mHelper.stop();
59 | }
60 |
61 | public void sendText(View view) {
62 | StringMessage message = new StringMessage();
63 | message.setContent("我是Service内容","扩展信息");
64 | mHelper.write(message);
65 |
66 | }
67 |
68 | public void sendImage(View view) {
69 | chooseImage();
70 | }
71 |
72 | @Override
73 | public void onChooseImage(String path) {
74 | File file = new File(path);
75 | ImageMessage message = new ImageMessage();
76 | message.setContent(file,file.getName());
77 | mHelper.write(message);
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/library_bluesocket/src/main/java/com/xloong/library/bluesocket/utils/TypeUtils.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket.utils;
2 |
3 |
4 | import android.util.Log;
5 |
6 | import com.xloong.library.bluesocket.message.IMessage;
7 | import com.xloong.library.bluesocket.message.ImageMessage;
8 | import com.xloong.library.bluesocket.message.StringMessage;
9 |
10 | import java.io.IOException;
11 | import java.io.InputStream;
12 | import java.nio.ByteBuffer;
13 | import java.util.ArrayList;
14 |
15 | /**
16 | * @author bingbing
17 | * @date 16/7/2
18 | */
19 | public class TypeUtils {
20 |
21 | //byte 数组与 long 的相互转换
22 | public static byte[] longToBytes(long x) {
23 | ByteBuffer buffer = ByteBuffer.allocate(8);
24 | buffer.putLong(0, x);
25 | return buffer.array();
26 | }
27 |
28 | public static long bytesToLong(byte[] bytes) {
29 | ByteBuffer buffer = ByteBuffer.allocate(8);
30 | buffer.put(bytes, 0, bytes.length);
31 | buffer.flip();//need flip
32 | return buffer.getLong();
33 | }
34 |
35 |
36 | /**
37 | * 从流里面读取
38 | *
39 | * @return
40 | * @throws IOException
41 | */
42 | public static IMessage readHeader(InputStream inputStream) throws IOException {
43 | ArrayList arrayList = new ArrayList<>();
44 | while (true) {
45 | int data = inputStream.read();
46 | Log.d("BLUE","data:"+data + "");
47 | if (data == 0x0A) { //\r 不增加
48 | Log.d("readHeader", "sss");
49 | } else if (data == 0x0D) { //\n
50 | if (arrayList.size() < 10) {
51 | arrayList.clear();
52 | continue;
53 | }
54 | byte[] buffer = new byte[arrayList.size()];
55 | for (int i = 0; i < arrayList.size(); i++) {
56 | buffer[i] = arrayList.get(i).byteValue();
57 | }
58 |
59 | IMessage iMessage = parseHeader(buffer);
60 | if (iMessage != null) {
61 | Log.d("message",iMessage.toString());
62 | return iMessage;
63 | } else {
64 | arrayList.clear();
65 | }
66 | } else {
67 | if (arrayList.size() == 0) {
68 | if (data != IMessage.HEADER) {
69 | continue;
70 | }
71 | }
72 | arrayList.add(data);
73 | }
74 | }
75 | }
76 |
77 |
78 | public static IMessage parseHeader(byte[] header) {
79 | if (header.length < 10) return null;
80 |
81 | IMessage message = null;
82 | byte tempType = header[1];
83 |
84 | if (tempType == IMessage.TYPE_BYTE) {
85 | message = new ImageMessage();
86 | } else if (tempType == IMessage.TYPE_String) {
87 | message = new StringMessage();
88 | } else {
89 | return null;
90 | }
91 |
92 | message.setType(tempType);
93 |
94 | byte[] length = new byte[8];
95 | System.arraycopy(header, 2, length, 0, 8);
96 | long mLength = TypeUtils.bytesToLong(length);
97 | String mExtend = new String(header, 10, header.length - 10);
98 | message.setLength(mLength);
99 | message.setExtend(mExtend);
100 | return message;
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/library_bluesocket/src/main/java/com/xloong/library/bluesocket/BlueManager.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket;
2 |
3 | import android.bluetooth.BluetoothDevice;
4 | import android.content.ComponentName;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.content.ServiceConnection;
8 | import android.os.IBinder;
9 | import android.os.RemoteException;
10 |
11 | import com.xloong.library.bluesocket.message.IMessage;
12 |
13 | /**
14 | * 蓝牙的管理类。
15 | * 用于连接蓝牙,发送消息等。
16 | * Created by xubingbing on 2017/8/10.
17 | */
18 | public class BlueManager {
19 |
20 | private static BlueManager mManager;
21 | private Context mContext;
22 | private IBluetooth mProxyService;
23 | private BlueManager(Context context){
24 | mContext = context;
25 | initService();
26 | }
27 |
28 | public static void init(Context context){
29 | if (mManager == null){
30 | synchronized (BlueManager.class){
31 | if (mManager == null){
32 | mManager = new BlueManager(context);
33 | }
34 | }
35 | }
36 | }
37 |
38 | public static BlueManager getInstance(){
39 | return mManager;
40 | }
41 |
42 | private void initService(){
43 | Intent intent = new Intent(mContext,BlueService.class);
44 |
45 | mContext.bindService(intent,mConnection,Context.BIND_AUTO_CREATE);
46 | }
47 |
48 | private ServiceConnection mConnection = new ServiceConnection() {
49 | @Override
50 | public void onServiceConnected(ComponentName name, IBinder service) {
51 | mProxyService = IBluetooth.Stub.asInterface(service);
52 | try {
53 | mProxyService.asBinder().linkToDeath(mDeathRecipient, 0);
54 | } catch (RemoteException e) {
55 | e.printStackTrace();
56 | }
57 | }
58 |
59 | @Override
60 | public void onServiceDisconnected(ComponentName name) {
61 | mProxyService = null;
62 | }
63 | };
64 |
65 |
66 | private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
67 | @Override
68 | public void binderDied() {
69 | mProxyService.asBinder().unlinkToDeath(mDeathRecipient, 0);
70 | mProxyService = null;
71 | initService();
72 | }
73 | };
74 |
75 |
76 | public boolean isConnection(){
77 | if (mProxyService != null){
78 | try {
79 | return mProxyService.isConnected();
80 | } catch (RemoteException e) {
81 | e.printStackTrace();
82 | }
83 | }
84 |
85 | return false;
86 | }
87 |
88 | /**
89 | * 启动蓝牙Service 端,此方法用于Service 端
90 | */
91 | public void startService(){
92 | if (mProxyService != null){
93 | try {
94 | mProxyService.startService();
95 | } catch (RemoteException e) {
96 | e.printStackTrace();
97 | }
98 | }
99 | }
100 |
101 | /**
102 | * 连接蓝牙 此方法用于Client 端
103 | * @param device
104 | */
105 | public void connection(BluetoothDevice device){
106 | if (mProxyService != null){
107 | try {
108 | mProxyService.connectionService(device);
109 | } catch (RemoteException e) {
110 | e.printStackTrace();
111 | }
112 | }
113 | }
114 |
115 |
116 | /**
117 | * 发送消息
118 | * @param message
119 | */
120 | public void sendMessage(IMessage message){
121 | Intent intent = new Intent(BlueService.ACTION_MESSAGE_SEND);
122 | intent.putExtra("message",message);
123 | mContext.sendBroadcast(intent);
124 | }
125 |
126 |
127 | }
128 |
--------------------------------------------------------------------------------
/library_bluesocket/src/main/java/com/xloong/library/bluesocket/message/StringMessage.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket.message;
2 |
3 | import android.os.Parcel;
4 | import android.text.TextUtils;
5 |
6 | import com.xloong.library.bluesocket.utils.TypeUtils;
7 |
8 | import java.io.IOException;
9 | import java.io.InputStream;
10 | import java.io.OutputStream;
11 | import java.util.Arrays;
12 |
13 | /**
14 | * Created by xubingbing on 2017/8/10.
15 | */
16 |
17 | public class StringMessage implements IMessage {
18 | private String mContent;
19 | private byte[] contentByte;
20 | private String mExtend;
21 | private long mLength;
22 | private byte mType = TYPE_String;
23 |
24 | @Override
25 | public byte getType() {
26 | return mType;
27 | }
28 |
29 | @Override
30 | public void setType(byte type) {
31 | mType = type;
32 | }
33 |
34 | @Override
35 | public long getLength() {
36 | return mLength;
37 | }
38 |
39 | @Override
40 | public void setLength(long length) {
41 | mLength = length;
42 | }
43 |
44 | @Override
45 | public String getContent() {
46 | return mContent;
47 | }
48 |
49 | @Override
50 | public void setExtend(String extend) {
51 | mExtend = extend;
52 | }
53 |
54 |
55 | @Override
56 | public void setContent(String content, String extend) {
57 | contentByte = content.getBytes();
58 | mLength = contentByte.length;
59 | if (TextUtils.isEmpty(extend)) {
60 | mExtend = "";
61 | } else {
62 | mExtend = extend;
63 | }
64 | }
65 |
66 | @Override
67 | public void parseContent(InputStream inputStream) throws IOException {
68 | contentByte = new byte[(int) mLength];
69 | inputStream.read(contentByte,0, (int) mLength);
70 | mContent = new String(contentByte);
71 | }
72 |
73 |
74 |
75 | @Override
76 | public void writeContent(OutputStream outputStream) throws IOException {
77 | outputStream.write(creatHeader());
78 | outputStream.write(0xA);
79 | outputStream.write(0xD);
80 | outputStream.write(contentByte);
81 | }
82 |
83 | @Override
84 | public byte[] creatHeader() {
85 | byte[] extend = mExtend.getBytes();
86 | byte[] length = TypeUtils.longToBytes(getLength());
87 | byte[] header = new byte[10 + extend.length];
88 | header[0] = HEADER; //魔数
89 | header[1] = getType(); //类型
90 | System.arraycopy(length, 0, header, 2, 8); //长度
91 | System.arraycopy(extend, 0, header, 10, extend.length); //扩展信息
92 | return header;
93 | }
94 |
95 | @Override
96 | public int describeContents() {
97 | return 0;
98 | }
99 |
100 | @Override
101 | public void writeToParcel(Parcel dest, int flags) {
102 | dest.writeString(this.mContent);
103 | dest.writeByteArray(this.contentByte);
104 | dest.writeString(this.mExtend);
105 | dest.writeLong(this.mLength);
106 | dest.writeByte(this.mType);
107 | }
108 |
109 | public StringMessage() {
110 | }
111 |
112 | protected StringMessage(Parcel in) {
113 | this.mContent = in.readString();
114 | this.contentByte = in.createByteArray();
115 | this.mExtend = in.readString();
116 | this.mLength = in.readLong();
117 | this.mType = in.readByte();
118 | }
119 |
120 | public static final Creator CREATOR = new Creator() {
121 | @Override
122 | public StringMessage createFromParcel(Parcel source) {
123 | return new StringMessage(source);
124 | }
125 |
126 | @Override
127 | public StringMessage[] newArray(int size) {
128 | return new StringMessage[size];
129 | }
130 | };
131 |
132 | @Override
133 | public String toString() {
134 | return "StringMessage{" +
135 | "mContent='" + mContent + '\'' +
136 | ", contentByte=" + Arrays.toString(contentByte) +
137 | ", mExtend='" + mExtend + '\'' +
138 | ", mLength=" + mLength +
139 | ", mType=" + mType +
140 | '}';
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/library_bluesocket/src/main/java/com/xloong/library/bluesocket/BlueService.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket;
2 |
3 | import android.app.Service;
4 | import android.bluetooth.BluetoothDevice;
5 | import android.content.BroadcastReceiver;
6 | import android.content.Context;
7 | import android.content.Intent;
8 | import android.content.IntentFilter;
9 | import android.os.Handler;
10 | import android.os.IBinder;
11 | import android.os.Message;
12 | import android.os.RemoteException;
13 |
14 | import com.xloong.library.bluesocket.message.IMessage;
15 |
16 | /**
17 | * Created by xubingbing on 2017/8/10.
18 | */
19 |
20 | public class BlueService extends Service implements BluetoothSppHelper.BlueSocketListener {
21 |
22 | public static final String ACTION_MESSAGE_REVEIVER = "com.xloong.library.bluesocket.message.receiver";
23 | public static final String ACTION_MESSAGE_SEND = "com.xloong.library.bluesocket.message.send";
24 | public static final int WHAT_CONNECT = 1;
25 | public static final int WHAT_START = 2;
26 | private BluetoothSppHelper mHelper;
27 | private boolean isService;
28 | private BluetoothDevice mDevice;
29 |
30 | private boolean isConnection = false;
31 | private Handler mHandler = new Handler(){
32 | @Override
33 | public void handleMessage(Message msg) {
34 | super.handleMessage(msg);
35 | switch (msg.what){
36 | case WHAT_CONNECT:
37 | isService = false;
38 | mDevice = (BluetoothDevice)msg.obj;
39 | mHelper.connect(mDevice);
40 | break;
41 |
42 | case WHAT_START:
43 | isService = true;
44 | mHelper.strat();
45 | break;
46 | }
47 | }
48 | };
49 |
50 | @Override
51 | public IBinder onBind(Intent intent) {
52 | return mBinder;
53 | }
54 |
55 |
56 | @Override
57 | public void onCreate() {
58 | super.onCreate();
59 | initReceiver();
60 | mHelper = new BluetoothSppHelper();
61 | mHelper.setBlueSocketListener(this);
62 |
63 | }
64 |
65 | @Override
66 | public void onBlueSocketStatusChange(BlueSocketBaseThread.BlueSocketStatus status, BluetoothDevice remoteDevice) {
67 | if (status == BlueSocketBaseThread.BlueSocketStatus.DISCONNECTION){
68 | mHandler.postDelayed(new Runnable() {
69 | @Override
70 | public void run() {
71 | if (isService){
72 | mHelper.strat();
73 | }else {
74 | if (mDevice != null){
75 | mHelper.connect(mDevice);
76 | }
77 | }
78 | }
79 | },1000);
80 | }
81 |
82 | if (status == BlueSocketBaseThread.BlueSocketStatus.CONNEDTIONED){
83 | isConnection = true;
84 | }else {
85 | isConnection = false;
86 | }
87 | }
88 |
89 | @Override
90 | public void onBlueSocketMessageReceiver(IMessage message) {
91 | Intent intent = new Intent(ACTION_MESSAGE_REVEIVER);
92 | intent.putExtra("message",message);
93 | sendBroadcast(intent);
94 | }
95 |
96 |
97 | private void initReceiver(){
98 | IntentFilter intentFilter = new IntentFilter();
99 | intentFilter.addAction(ACTION_MESSAGE_SEND);
100 | registerReceiver(mReceiver,intentFilter);
101 | }
102 | private BroadcastReceiver mReceiver = new BroadcastReceiver() {
103 | @Override
104 | public void onReceive(Context context, Intent intent) {
105 | switch (intent.getAction()){
106 | case ACTION_MESSAGE_SEND:
107 | if (mHelper != null){
108 | IMessage message = intent.getParcelableExtra("message");
109 | mHelper.write(message);
110 | }
111 | break;
112 | }
113 | }
114 | };
115 |
116 |
117 | @Override
118 | public void onDestroy() {
119 | unregisterReceiver(mReceiver);
120 | super.onDestroy();
121 | }
122 |
123 |
124 | private IBluetooth.Stub mBinder = new IBluetooth.Stub() {
125 | @Override
126 | public boolean isConnected() throws RemoteException {
127 | return isConnection;
128 | }
129 |
130 | @Override
131 | public void startService() throws RemoteException {
132 | mHandler.obtainMessage(WHAT_START).sendToTarget();
133 | }
134 |
135 | @Override
136 | public void connectionService(BluetoothDevice device) throws RemoteException {
137 | mHandler.obtainMessage(WHAT_CONNECT,device).sendToTarget();
138 | }
139 | };
140 | }
141 |
--------------------------------------------------------------------------------
/library_bluesocket/src/main/java/com/xloong/library/bluesocket/BlueDataThread.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket;
2 |
3 | import android.bluetooth.BluetoothSocket;
4 | import android.os.Handler;
5 | import android.os.SystemClock;
6 | import android.util.Log;
7 |
8 | import com.xloong.library.bluesocket.message.IMessage;
9 | import com.xloong.library.bluesocket.utils.TypeUtils;
10 |
11 | import java.io.IOException;
12 | import java.io.InputStream;
13 | import java.io.OutputStream;
14 | import java.util.concurrent.ConcurrentLinkedQueue;
15 |
16 | /**
17 | * 服务端和客户端共用的数据传输线程
18 | *
19 | * @author bingbing
20 | * @date 16/4/6
21 | */
22 | public class BlueDataThread extends BlueSocketBaseThread {
23 |
24 |
25 | private BluetoothSocket mBlueSocket;
26 | private OutputStream mBlueSocketOutputStream;
27 | private InputStream mBlueSocketInputStream;
28 | private ConcurrentLinkedQueue mQueue;
29 | private boolean isSendFinish = true; //数据是否发送完成
30 | private SendMessageThread mSendThread;
31 |
32 | public BlueDataThread(ConcurrentLinkedQueue queue, BluetoothSocket bluetoothSocket, Handler handler) {
33 | super(handler);
34 | mBlueSocket = bluetoothSocket;
35 | mQueue = queue;
36 | }
37 |
38 |
39 | @Override
40 | public void run() {
41 | super.run();
42 | if (!isRunning) return;
43 | try {
44 | mBlueSocketOutputStream = mBlueSocket.getOutputStream();
45 | mBlueSocketInputStream = mBlueSocket.getInputStream();
46 | sendMessage(BlueSocketStatus.CONNEDTIONED, mBlueSocket.getRemoteDevice());
47 | } catch (IOException e) {
48 | e.printStackTrace();
49 | sendMessage(BlueSocketStatus.DISCONNECTION);
50 | return;
51 | }
52 |
53 | //监听通道后,死循环监听通道,去读取message ,
54 | while (isRunning) {
55 | try {
56 | IMessage message = TypeUtils.readHeader(mBlueSocketInputStream);
57 | if (message != null){
58 | message.parseContent(mBlueSocketInputStream);
59 | sendMessage(BlueSocketStatus.MESSAGERECEIVE, message);
60 | Log.d("BLUE","完成");
61 | }
62 | } catch (Exception e) {
63 | e.printStackTrace();
64 | sendMessage(BlueSocketStatus.DISCONNECTION);
65 | return;
66 | }
67 |
68 | }
69 | }
70 |
71 |
72 | /**
73 | * 开始消息队列
74 | */
75 | public synchronized void startQueue() {
76 | if (mQueue != null && !mQueue.isEmpty() && isSendFinish) {
77 | if (mSendThread != null) {
78 | mSendThread.cancle();
79 | }
80 | mSendThread = new SendMessageThread();
81 | mSendThread.start();
82 | }
83 |
84 | }
85 |
86 |
87 | @Override
88 | public BluetoothSocket getSocket() {
89 | return null;
90 | }
91 |
92 | @Override
93 | public void cancle() {
94 | super.cancle();
95 |
96 | if (mSendThread != null) {
97 | mSendThread.cancle();
98 | mSendThread = null;
99 | mQueue = null;
100 | }
101 | try {
102 | if (mBlueSocketInputStream != null)
103 | mBlueSocketInputStream.close();
104 | } catch (IOException e) {
105 | e.printStackTrace();
106 | }
107 |
108 | try {
109 | if (mBlueSocket != null)
110 | mBlueSocket.close();
111 | } catch (IOException e) {
112 | e.printStackTrace();
113 | }
114 |
115 | try {
116 | if (mBlueSocketOutputStream != null)
117 | mBlueSocketOutputStream.close();
118 | } catch (IOException e) {
119 | e.printStackTrace();
120 | }
121 |
122 | mBlueSocketInputStream = null;
123 | mBlueSocketOutputStream = null;
124 | mBlueSocket = null;
125 |
126 | }
127 |
128 | private class SendMessageThread extends Thread {
129 | private boolean isCancle = false;
130 |
131 | @Override
132 | public void run() {
133 | super.run();
134 | isSendFinish = false;
135 | while (mQueue != null && !mQueue.isEmpty() && !isCancle) {
136 | try {
137 | Log.d("BlueDataThread", "开始发消息给客户端");
138 | IMessage messageObject = mQueue.poll();
139 | messageObject.writeContent(mBlueSocketOutputStream);
140 | Log.d("BlueDataThread", "发消息给客户端成功");
141 | } catch (IOException e) {
142 | e.printStackTrace();
143 | sendMessage(BlueSocketStatus.DISCONNECTION);
144 | isCancle = true;
145 | }
146 | SystemClock.sleep(50);
147 | }
148 | isSendFinish = true;
149 | }
150 |
151 | public void cancle() {
152 | isCancle = true;
153 | }
154 |
155 |
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xloong/bluetoothsocketdemo/ClientActivity.java:
--------------------------------------------------------------------------------
1 | package com.xloong.bluetoothsocketdemo;
2 |
3 | import android.app.Activity;
4 | import android.bluetooth.BluetoothAdapter;
5 | import android.bluetooth.BluetoothDevice;
6 | import android.content.BroadcastReceiver;
7 | import android.content.Context;
8 | import android.content.Intent;
9 | import android.content.IntentFilter;
10 | import android.os.Bundle;
11 | import android.view.View;
12 | import android.view.ViewGroup;
13 | import android.widget.AdapterView;
14 | import android.widget.BaseAdapter;
15 | import android.widget.EditText;
16 | import android.widget.ListView;
17 | import android.widget.TextView;
18 | import android.widget.Toast;
19 |
20 | import com.xloong.library.bluesocket.BlueSocketBaseThread;
21 | import com.xloong.library.bluesocket.BluetoothSppHelper;
22 | import com.xloong.library.bluesocket.message.IMessage;
23 | import com.xloong.library.bluesocket.message.ImageMessage;
24 | import com.xloong.library.bluesocket.message.StringMessage;
25 |
26 | import java.io.File;
27 | import java.util.ArrayList;
28 | import java.util.List;
29 |
30 | /**
31 | * @author bingbing
32 | * @date 16/4/7
33 | */
34 | public class ClientActivity extends BaseActivity implements BluetoothSppHelper.BlueSocketListener, AdapterView.OnItemClickListener {
35 |
36 | private EditText mEdit;
37 | private TextView mConnectionStatus;
38 | private ListView mList;
39 | private BluetoothSppHelper mHelper;
40 | private List devices = new ArrayList<>();
41 | private BaseAdapter mBlueAdapter;
42 |
43 | @Override
44 | protected void onCreate(Bundle savedInstanceState) {
45 | super.onCreate(savedInstanceState);
46 | setContentView(R.layout.activity_client);
47 | mEdit = (EditText) findViewById(R.id.edit);
48 | mList = (ListView) findViewById(R.id.list);
49 | mConnectionStatus = (TextView) findViewById(R.id.text);
50 | mHelper = new BluetoothSppHelper();
51 | mHelper.setBlueSocketListener(this);
52 | devices.addAll(BluetoothAdapter.getDefaultAdapter().getBondedDevices());
53 | mBlueAdapter = new MyAdapter();
54 | mList.setAdapter(mBlueAdapter);
55 |
56 | IntentFilter intentFilter = new IntentFilter();
57 | intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
58 | registerReceiver(mReceiver, intentFilter);
59 | mList.setOnItemClickListener(this);
60 | }
61 |
62 | public void find(View view) {
63 | BluetoothAdapter.getDefaultAdapter().startDiscovery();
64 | }
65 |
66 | public void send(View view) {
67 | // mHelper.write(mEdit.getText().toString());
68 | }
69 |
70 |
71 | public void sendText(View view) {
72 | StringMessage message = new StringMessage();
73 | message.setContent("我是Client内容","扩展信息");
74 | mHelper.write(message);
75 | }
76 |
77 | public void sendImage(View view) {
78 | chooseImage();
79 | }
80 |
81 |
82 | @Override
83 | public void onBlueSocketStatusChange(BlueSocketBaseThread.BlueSocketStatus status, BluetoothDevice device) {
84 | mConnectionStatus.setText(status.toString());
85 | }
86 |
87 | @Override
88 | public void onBlueSocketMessageReceiver(IMessage message) {
89 | if (message instanceof StringMessage){
90 | Toast.makeText(this, ((StringMessage)message).getContent(), Toast.LENGTH_SHORT).show();
91 | }else if (message instanceof ImageMessage){
92 | Toast.makeText(this, ((ImageMessage)message).getContent().getAbsolutePath(), Toast.LENGTH_SHORT).show();
93 | }
94 |
95 | }
96 |
97 |
98 | private BroadcastReceiver mReceiver = new BroadcastReceiver() {
99 | @Override
100 | public void onReceive(Context context, Intent intent) {
101 | devices.add((BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE));
102 | mBlueAdapter.notifyDataSetChanged();
103 | }
104 | };
105 |
106 | @Override
107 | protected void onDestroy() {
108 | super.onDestroy();
109 | mHelper.stop();
110 | unregisterReceiver(mReceiver);
111 | }
112 |
113 | @Override
114 | public void onItemClick(AdapterView> parent, View view, int position, long id) {
115 | BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
116 | mHelper.connect(devices.get(position));
117 | }
118 |
119 | @Override
120 | public void onChooseImage(String path) {
121 | File file = new File(path);
122 | ImageMessage message = new ImageMessage();
123 | message.setContent(new File(path),file.getName());
124 | mHelper.write(message);
125 | }
126 |
127 |
128 | private class MyAdapter extends BaseAdapter {
129 |
130 | @Override
131 | public int getCount() {
132 | return devices.size();
133 | }
134 |
135 | @Override
136 | public Object getItem(int position) {
137 | return devices.get(position);
138 | }
139 |
140 | @Override
141 | public long getItemId(int position) {
142 | return position;
143 | }
144 |
145 | @Override
146 | public View getView(int position, View convertView, ViewGroup parent) {
147 | TextView text = new TextView(parent.getContext());
148 | text.setText(devices.get(position).getName());
149 | return text;
150 | }
151 | }
152 | }
153 |
--------------------------------------------------------------------------------
/library_bluesocket/src/main/java/com/xloong/library/bluesocket/BluetoothSppHelper.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket;
2 |
3 | import android.bluetooth.BluetoothDevice;
4 | import android.os.Handler;
5 | import android.os.Message;
6 | import android.util.Log;
7 |
8 | import com.xloong.library.bluesocket.BlueSocketBaseThread.BlueSocketStatus;
9 | import com.xloong.library.bluesocket.message.IMessage;
10 |
11 | import java.util.concurrent.ConcurrentLinkedQueue;
12 |
13 | /**
14 | * @author bingbing
15 | * @date 16/4/6
16 | */
17 | public class BluetoothSppHelper {
18 |
19 | private BlueSocketBaseThread mTargThread;
20 | private BlueDataThread mDataThread;
21 | private BlueSocketBaseThread.BlueSocketStatus mNowStatus = BlueSocketBaseThread.BlueSocketStatus.NONE;
22 | private BlueSocketListener mStatusListener;
23 | /**
24 | * 消息队列
25 | */
26 | private ConcurrentLinkedQueue mQueue = new ConcurrentLinkedQueue<>();
27 |
28 |
29 | public BluetoothSppHelper() {
30 | }
31 |
32 | /**
33 | * 开始服务端监听线程,等待客户端连接
34 | */
35 | public void strat() {
36 | if (mTargThread != null) {
37 | mTargThread.cancle();
38 | mTargThread = null;
39 | }
40 |
41 | if (mDataThread != null) {
42 | mDataThread.cancle();
43 | mDataThread = null;
44 | }
45 |
46 | mTargThread = new BlueServiceThread(mSocketHandler);
47 | mTargThread.start();
48 | }
49 |
50 | /**
51 | * 客户端主动发起连接,去连接服务端
52 | *
53 | * @param serviceDevice 服务端的蓝牙设备
54 | */
55 | public void connect(BluetoothDevice serviceDevice) {
56 | if (mTargThread != null) {
57 | mTargThread.cancle();
58 | }
59 |
60 | if (mDataThread != null) {
61 | mDataThread.cancle();
62 | }
63 | mTargThread = new BlueClientThread(serviceDevice, mSocketHandler);
64 | mTargThread.start();
65 | }
66 |
67 |
68 | public synchronized boolean write(IMessage message) {
69 | message.toString();
70 |
71 | if (mNowStatus == BlueSocketBaseThread.BlueSocketStatus.CONNEDTIONED) {
72 | synchronized (BluetoothSppHelper.class) {
73 | if (mNowStatus == BlueSocketBaseThread.BlueSocketStatus.CONNEDTIONED) {
74 | mQueue.add(message);
75 | mDataThread.startQueue();
76 | return true;
77 | }
78 | return false;
79 | }
80 | }
81 | return false;
82 | }
83 |
84 |
85 |
86 | private Handler mSocketHandler = new Handler() {
87 |
88 | @Override
89 | public void handleMessage(Message msg) {
90 | super.handleMessage(msg);
91 | BlueSocketStatus status = BlueSocketStatus.values()[msg.what];
92 | synchronized (BluetoothSppHelper.class) {
93 | if (status != BlueSocketStatus.MESSAGERECEIVE) {
94 | mNowStatus = status;
95 | if (mStatusListener != null) {
96 | mStatusListener.onBlueSocketStatusChange(status, (BluetoothDevice) msg.obj);
97 | }
98 | }
99 |
100 | switch (BlueSocketStatus.values()[msg.what]) {
101 | case ACCEPTED: //当服务端或者客户端监听到对方已经同意连接,则分别开启数据通道线程,接受数据
102 | mQueue.clear();
103 | mDataThread = new BlueDataThread(mQueue, mTargThread.getSocket(), this);
104 | mDataThread.start();
105 | break;
106 | case DISCONNECTION:
107 | //如果连接断开,则停止数据通道线程
108 | if (mDataThread != null) {
109 | mDataThread.cancle();
110 | mDataThread = null;
111 | }
112 | break;
113 | case MESSAGERECEIVE:
114 | IMessage message = (IMessage) msg.obj;
115 | if (mStatusListener != null) {
116 | mStatusListener.onBlueSocketMessageReceiver(message);
117 | }
118 | break;
119 |
120 | }
121 |
122 | }
123 | }
124 | };
125 |
126 |
127 | public void setBlueSocketListener(BlueSocketListener statusListener) {
128 | mStatusListener = statusListener;
129 | }
130 |
131 |
132 | public interface BlueSocketListener {
133 |
134 | /**
135 | * 蓝牙socket连接状态该改变
136 | * 连接成功的时候,device 是代表连接的设备,其他状态remoteDevice 为null
137 | *
138 | * @param status
139 | */
140 | void onBlueSocketStatusChange(BlueSocketStatus status, BluetoothDevice remoteDevice);
141 |
142 | /**
143 | * 蓝牙socket消息到达
144 | */
145 | void onBlueSocketMessageReceiver(IMessage message);
146 | }
147 |
148 | /**
149 | * 出去时候停止掉所有线程
150 | */
151 | public void stop() {
152 | synchronized (BluetoothSppHelper.class){
153 | if (mTargThread != null) {
154 | mTargThread.cancle();
155 | mTargThread = null;
156 | }
157 |
158 | if (mDataThread != null) {
159 | mDataThread.cancle();
160 | mDataThread = null;
161 | }
162 |
163 | if (mStatusListener != null) {
164 | mStatusListener.onBlueSocketStatusChange(BlueSocketStatus.DISCONNECTION, null);
165 | }
166 | }
167 |
168 |
169 | }
170 |
171 |
172 | }
173 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/library_bluesocket/src/main/java/com/xloong/library/bluesocket/message/ImageMessage.java:
--------------------------------------------------------------------------------
1 | package com.xloong.library.bluesocket.message;
2 |
3 | import android.os.Environment;
4 | import android.os.Parcel;
5 |
6 | import com.xloong.library.bluesocket.utils.TypeUtils;
7 |
8 | import java.io.File;
9 | import java.io.FileInputStream;
10 | import java.io.FileNotFoundException;
11 | import java.io.FileOutputStream;
12 | import java.io.IOException;
13 | import java.io.InputStream;
14 | import java.io.OutputStream;
15 |
16 | import static android.os.Environment.DIRECTORY_PICTURES;
17 |
18 | /**
19 | * Created by xubingbing on 2017/8/10.
20 | */
21 |
22 | public class ImageMessage implements IMessage {
23 |
24 | private File mContent;
25 | private String mExtend;
26 | private long mLength;
27 | private byte mType = TYPE_BYTE;
28 |
29 | public ImageMessage() {
30 |
31 | }
32 |
33 | @Override
34 | public byte getType() {
35 | return mType;
36 | }
37 |
38 | @Override
39 | public void setType(byte type) {
40 | mType = type;
41 | }
42 |
43 | @Override
44 | public long getLength() {
45 | return mLength;
46 | }
47 |
48 | @Override
49 | public void setLength(long length) {
50 | mLength = length;
51 | }
52 |
53 | @Override
54 | public File getContent() {
55 | return mContent;
56 | }
57 |
58 | @Override
59 | public void setExtend(String extend) {
60 | mExtend = extend;
61 | }
62 |
63 |
64 | @Override
65 | public void setContent(File content, String extend) {
66 | mContent = content;
67 | mExtend = extend;
68 | try {
69 | FileInputStream fio = new FileInputStream(mContent);
70 | mLength = fio.available();
71 | fio.close();
72 | } catch (FileNotFoundException e) {
73 | e.printStackTrace();
74 | } catch (IOException e) {
75 | e.printStackTrace();
76 | }
77 |
78 | }
79 |
80 | @Override
81 | public void parseContent(InputStream inputStream) throws IOException {
82 | mContent = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES), mExtend);
83 |
84 | if (!mContent.getParentFile().exists()) {
85 | mContent.getParentFile().mkdirs();
86 | }
87 | if (mContent.exists()) {
88 | mContent.createNewFile();
89 | }
90 | FileOutputStream fos = new FileOutputStream(mContent);
91 |
92 |
93 | long tempLength = 0;
94 | byte[] buffer = null;
95 | while (tempLength < mLength) {
96 | if (mLength - tempLength < (1024 * 64)) {
97 | buffer = new byte[(int) (mLength - tempLength)];
98 | } else {
99 | if (buffer == null)
100 | buffer = new byte[1024 * 64];
101 | }
102 | int readLength = inputStream.read(buffer, 0, buffer.length);
103 | fos.write(buffer, 0, readLength);
104 | tempLength += readLength;
105 | }
106 |
107 | fos.close();
108 | }
109 |
110 |
111 | @Override
112 | public void writeContent(OutputStream outputStream) throws IOException {
113 | outputStream.write(creatHeader());
114 | outputStream.write(0xA);
115 | outputStream.write(0xD);
116 |
117 | FileInputStream fio = new FileInputStream(mContent);
118 |
119 |
120 | byte[] buffer = new byte[64 * 1024];
121 | int length = 0;
122 | while ((length = fio.read(buffer)) >= 0) {
123 | outputStream.write(buffer, 0, length);
124 | outputStream.flush();
125 | }
126 |
127 | }
128 |
129 | @Override
130 | public byte[] creatHeader() {
131 | byte[] extend = mExtend.getBytes();
132 | byte[] length = TypeUtils.longToBytes(getLength());
133 | byte[] header = new byte[10 + extend.length];
134 | header[0] = HEADER; //魔数
135 | header[1] = getType(); //类型
136 | System.arraycopy(length, 0, header, 2, length.length); //长度
137 | System.arraycopy(extend, 0, header, 10, extend.length); //扩展信息
138 | return header;
139 | }
140 |
141 | @Override
142 | public int describeContents() {
143 | return 0;
144 | }
145 |
146 | @Override
147 | public void writeToParcel(Parcel dest, int flags) {
148 | dest.writeString(this.mContent.getAbsolutePath());
149 | dest.writeString(this.mExtend);
150 | dest.writeLong(this.mLength);
151 | dest.writeByte(this.mType);
152 | }
153 |
154 | protected ImageMessage(Parcel in) {
155 | this.mContent = new File(in.readString());
156 | this.mExtend = in.readString();
157 | this.mLength = in.readLong();
158 | this.mType = in.readByte();
159 | }
160 |
161 | public static final Creator CREATOR = new Creator() {
162 | @Override
163 | public ImageMessage createFromParcel(Parcel source) {
164 | return new ImageMessage(source);
165 | }
166 |
167 | @Override
168 | public ImageMessage[] newArray(int size) {
169 | return new ImageMessage[size];
170 | }
171 | };
172 |
173 | @Override
174 | public String toString() {
175 | return "ImageMessage{" +
176 | "mContent=" + mContent +
177 | ", mExtend='" + mExtend + '\'' +
178 | ", mLength=" + mLength +
179 | ", mType=" + mType +
180 | '}';
181 | }
182 | }
183 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------