All Projects → sdwfqin → Classicbluetooth

sdwfqin / Classicbluetooth

Licence: apache-2.0
Android经典蓝牙工具类(cbt)------开关蓝牙、扫描蓝牙设备、连接配对、发送数据、接收数据、获取已配对设备

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Classicbluetooth

Drivety
Drivety: Smart App Assistant to Secure Inside Car Activity. #AndroidDevChallenge
Stars: ✭ 62 (-28.74%)
Mutual labels:  bluetooth
React Native Bluetooth Classic
⚛ Bluetooth classic Android(Bluetooth)/IOS(ExternalAccessory) module for serial communication
Stars: ✭ 74 (-14.94%)
Mutual labels:  bluetooth
Easyble
Multi-devices process Bluetooth library for Android
Stars: ✭ 81 (-6.9%)
Mutual labels:  bluetooth
Node Web Bluetooth
Web Bluetooth API and interactive device picker for node.js
Stars: ✭ 64 (-26.44%)
Mutual labels:  bluetooth
Rxbluetoothkotlin
Bluetooth low energy reactive framework for Android written in Kotlin
Stars: ✭ 68 (-21.84%)
Mutual labels:  bluetooth
Bluetooth
Pure Swift Bluetooth library
Stars: ✭ 74 (-14.94%)
Mutual labels:  bluetooth
Apple Family
A simple framework that brings Apple devices together - like a family
Stars: ✭ 59 (-32.18%)
Mutual labels:  bluetooth
Arduino Robust Serial
A simple and robust serial communication protocol. It was designed for Arduino but can be used for other purposes (e.g. bluetooth, sockets). Implementation in C Arduino, C++, Python and Rust.
Stars: ✭ 83 (-4.6%)
Mutual labels:  bluetooth
Livebudscli
A tool to control your Galaxy buds+ and live from linux
Stars: ✭ 69 (-20.69%)
Mutual labels:  bluetooth
Easybluetoothframe
经典(传统)蓝牙快速开发框架,A fast develop frame of classic bluetooth
Stars: ✭ 79 (-9.2%)
Mutual labels:  bluetooth
React Native Esc Pos
A React Native ESC/POS module to help you connect to your ESC/POS printer easily.
Stars: ✭ 65 (-25.29%)
Mutual labels:  bluetooth
Hibeacons
An iBeacons example app for iOS 10, with Apple Watch (watchOS 3.0) support, written in Swift 3.
Stars: ✭ 1,153 (+1225.29%)
Mutual labels:  bluetooth
Ha Bt Proximity
Distributed Bluetooth Room Presence Sensor for Home Assistant
Stars: ✭ 77 (-11.49%)
Mutual labels:  bluetooth
Qdomyos Zwift
Zwift bridge for smart treadmills and bike/cyclette
Stars: ✭ 63 (-27.59%)
Mutual labels:  bluetooth
Pulsator
Pulse animation for iOS
Stars: ✭ 1,238 (+1322.99%)
Mutual labels:  bluetooth
Tastysnake
A two-player (Bluetooth) game on Android.
Stars: ✭ 61 (-29.89%)
Mutual labels:  bluetooth
Cve 2017 0781
Blueborne CVE-2017-0781 Android heap overflow vulnerability
Stars: ✭ 74 (-14.94%)
Mutual labels:  bluetooth
Spike Prime
Experiments with the LEGO Mindstorms (51515) and SPIKE Prime (45678)
Stars: ✭ 85 (-2.3%)
Mutual labels:  bluetooth
Nrf52dk Sys
A Rust Crate to develop on the Nordic nRF52-DK
Stars: ✭ 83 (-4.6%)
Mutual labels:  bluetooth
Rxbluetoothkit
iOS & OSX Bluetooth library for RxSwift
Stars: ✭ 1,213 (+1294.25%)
Mutual labels:  bluetooth

ClassicBluetooth

Android经典蓝牙工具类

已完成:

  • 开启蓝牙
  • 关闭蓝牙
  • 扫描蓝牙设备
  • 连接配对
  • 发送数据
  • 接收数据
  • 获取已配对设备

TODO:

  • 接收文件
  • 功能优化、性能优化、BUG清扫

最近在做一个带有蓝牙打印机的项目,借此完成这个库的开发

导入

implementation 'com.sdwfqin.cbt:cbt:1.1.1'

使用

  1. 初始化

    public class BaseApp extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            CbtManager
                    .getInstance()
                    // 初始化
                    .init(this)
                    // 是否打印相关日志
                    .enableLog(true);
        }
    }
    
    /**
     * 设置自定义UUID
     *
     * @param uuid
     * @return BleManager
     */
    public CbtManager setUUID(String uuid) {
        CbtConstant.CBT_UUID = UUID.fromString(uuid);
        return this;
    }
    
    /**
     * 设置自定义服务名称
     *
     * @param name
     * @return BleManager
     */
    public CbtManager setServiceName(String name) {
        CbtConstant.CBT_NAME = name;
        return this;
    }
    
  2. 开启蓝牙

    CbtManager
        .getInstance()
        .enableBluetooth(isOn -> {
            if (isOn) {
                Toast.makeText(mContext, "蓝牙已开启", Toast.LENGTH_SHORT).show();
            }
        });
    
  3. 关闭蓝牙

    CbtManager
        .getInstance()
        .disableBluetooth(isOn -> {
            if (!isOn) {
                Toast.makeText(mContext, "蓝牙已关闭", Toast.LENGTH_SHORT).show();
            }
        });
    
  4. 扫描设备

    CbtManager
        .getInstance()
        .scan(new ScanCallback() {
            @Override
            public void onScanStart(boolean isOn) {
                // 开始扫描
            }
    
            @Override
            public void onScanStop(List<BluetoothDevice> devices) {
                // 搜索完成
                mScanListAdapter.setNewData(devices);
            }
    
            @Override
            public void onFindDevice(BluetoothDevice device) {
                // 搜索到设备
                mScanListAdapter.addData(device);
            }
        });
    
  5. 连接设备

    BluetoothDevice item = mScanListAdapter.getItem(position);
    CbtManager
        .getInstance()
        .connectDevice(item, new ConnectDeviceCallback() {
            @Override
            public void connectSuccess(BluetoothSocket socket, BluetoothDevice device) {
                // 连接成功
                Toast.makeText(mContext, "连接成功!", Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void connectError(Throwable throwable) {
                // 连接失败
                Toast.makeText(mContext, "连接失败:" + throwable.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    
  6. 发送数据

    byte[] data;
    try {
        data = (mData.getText().toString() + "\n\n\n\n\n\n").getBytes("GBK");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return;
    }
    
    List<byte[]> bytes = new ArrayList<>();
    bytes.add(BYTES[0]);
    bytes.add(BYTES[1]);
    bytes.add(data);
    
    CbtManager
        .getInstance()
        .sendData(bytes, new SendDataCallback() {
            @Override
            public void sendSuccess() {
                // 发送成功
            }
    
            @Override
            public void sendError(Throwable throwable) {
                // 发送失败
                Toast.makeText(SendDataActivity.this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    
  7. 蓝牙服务端(回调接口目前是在子线程中调用)

    CbtManager
        .getInstance()
        .startServiceListener(new ServiceListenerCallback() {
            @Override
            public void onStartError(Throwable throwable) {
                // 发生错误
                CbtLogs.e(throwable.getMessage());
            }
    
            @Override
            public void onDataListener(String s, BluetoothDevice device) {
                // 获取到数据
                runOnUiThread(() ->
                        mReceiveDataAdapter.addData(new ReceiveDataModel(device, s))
                );
            }
        });
    

License

Copyright 2018 zhangqin

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].