RxBluetooth一个很好的开源蓝牙操作类(扫描、状态监听、连接、发送、接收数据)

RxBluetooth一个很好的开源蓝牙操作类(扫描、状态监听、连接、发送、接收数据)

RxBluetooth

Android 蓝牙操作库. RxBluetooth

使用:

声明权限:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

创建RxBluetooth 实例.

检查蓝牙是否启动并开始使用:

if  (!rxBluetooth.isBluetoothAvailable()) {
  // to enable blutooth via startActivityForResult()
  rxBluetooth.enableBluetooth(this, REQUEST_ENABLE_BT);
}

Have fun.

确保 OnDestroy()中停止搜索与解绑:

if (rxBluetooth != null) {
     rxBluetooth.cancelDiscovery();
   }
unsubscribe(rxBluetoothSubscription);

观察者

rxBluetooth.observeDevices()
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .subscribe(new Action1<BluetoothDevice>() {
        @Override public void call(BluetoothDevice bluetoothDevice) {
          //
        }
      });

连接设备UUID可以定义或者通过反射获取通用UUID

// Use 00001101-0000-1000-8000-00805F9B34FB for SPP service 
// (ex. Arduino) or use your own generated UUID.
UUID uuid = UUID.fromString("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");

rxBluetooth.observeConnectDevice(bluetoothDevice, uuid)
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.io())
      .subscribe(new Action1<BluetoothSocket>() {
        @Override public void call(BluetoothSocket socket) {
          // Connected to the device, do anything with the socket 
        }
      }, new Action1<Throwable>() {
       @Override public void call(Throwable throwable) {
         // Error occured
       }
     });

观察扫描设备状态

扫描观察者:

rxBluetooth.observeDiscovery()
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .filter(Action.isEqualTo(BluetoothAdapter.ACTION_DISCOVERY_STARTED))
      .subscribe(new Action1<String>() {
        @Override public void call(String action) {
          //
        }
      });

To observe both ACTION_DISCOVERY_STARTED and ACTION_DISCOVERY_FINISHED:

rxBluetooth.observeDiscovery()
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .filter(Action.isEqualTo(BluetoothAdapter.ACTION_DISCOVERY_STARTED, BluetoothAdapter.ACTION_DISCOVERY_FINISHED))
      .subscribe(new Action1<String>() {
        @Override public void call(String action) {
          //
        }
      });

Observing蓝牙状态

rxBluetooth.observeBluetoothState()
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .filter(Action.isEqualTo(BluetoothAdapter.STATE_ON))
      .subscribe(new Action1<Integer>() {
        @Override public void call(Integer integer) {
          //
        }
      });

可以观察以下状态:

BluetoothAdapter.STATE_OFF  蓝牙关闭
BluetoothAdapter.STATE_TURNING_ON 蓝牙正在打开
BluetoothAdapter.STATE_ON  蓝牙打开
BluetoothAdapter.STATE_TURNING_OFF  蓝牙正在关闭

Observing扫描方式

rxBluetooth.observeScanMode()
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .filter(Action.isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE))
      .subscribe(new Action1<Integer>() {
        @Override public void call(Integer integer) {
          //
        }
      });

蓝牙扫描状态:

BluetoothAdapter.SCAN_MODE_NONE  蓝牙不能扫描以及被扫描
BluetoothAdapter.SCAN_MODE_CONNECTABLE 蓝牙可以扫描其他蓝牙设备
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE 蓝牙设备同时可以扫码其他蓝牙设备,并且可以被其他蓝牙设备扫描到

获取蓝牙对象暂不支持获取串口设备

rxBluetooth.observeBluetoothProfile(myProfile)
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .subscribe(new Action1<ServiceEvent>() {
        @Override public void call(ServiceEvent serviceEvent) {
          switch (serviceEvent.getState()) {
           case CONNECTED:
                BluetoothProfile bluetoothProfile = serviceEvent.getBluetoothProfile();
                List<BluetoothDevice> devices = bluetoothProfile.getConnectedDevices();                        
                for ( final BluetoothDevice dev : devices ) {
                  //..
                }
                break;
           case DISCONNECTED:
                //serviceEvent.getBluetoothProfile() returns null
                break;
            }
          }
        });

可支持以下协议设备,注意设备类型

myProfile can be one of BluetoothProfile.HEALTH, BluetoothProfile.HEADSET, BluetoothProfile.A2DP, BluetoothProfile.GATT or BluetoothProfile.GATT_SERVER

Clients should close profile proxy when they are no longer using the proxy obtained from observeBluetoothProfile:

rxBluetooth.closeProfileProxy(int profile, BluetoothProfile proxy);

读写 BluetoothSocket
创建连接后可以通过 BluetoothConnection 类用socket进行读写操作.

读:

BluetoothConnection bluetoothConnection = new BluetoothConnection(bluetoothSocket);

// Observe every byte received
bluetoothConnection.observeByteStream()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.io())
    .subscribe(new Action1<Byte>() {
      @Override public void call(Byte aByte) {
        // This will be called every single byte received
      }
    }, new Action1<Throwable>() {
      @Override public void call(Throwable throwable) {
        // Error occured
      }
    });

// Or just observe string
bluetoothConnection.observeStringStream()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.io())
    .subscribe(new Action1<String>() {
      @Override public void call(String string) {
        // This will be called every string received
      }
    }, new Action1<Throwable>() {
      @Override public void call(Throwable throwable) {
        // Error occured
      }
    });

写:

bluetoothConnection.send("Hello"); // String
bluetoothConnection.send("There".getBytes()); // Array of bytes

Download

compile 'com.github.ivbaranov:rxbluetooth:0.1.2'

Snapshots of the development version are available in Sonatype’s snapshots repository.

Contributing

Make sure you use SquareAndroid code style. ( https://github.com/square/java-code-styles)

Create a branch for each feature.

Developed By

Ivan Baranov

License

Copyright 2015 Ivan Baranov

Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值