Android 蓝牙通信——AndroidBluetoothManager


转载请说明出处!
作者:kqw攻城狮
出处:个人站 | CSDN


To get a Git project into your build:

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
        compile 'com.github.kongqw:AndroidBluetoothManager:1.0.0'
}

AndroidBluetoothManager

效果图

这里写图片描述

PNG

GIF

基础功能

添加权限

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

初始化

mBluetoothManager = new BluetoothManager();

打开蓝牙

mBluetoothManager.openBluetooth();

关闭蓝牙

mBluetoothManager.closeBluetooth();

添加蓝牙开关状态的监听

mBluetoothManager.setOnBluetoothStateListener(this);
/**
 * 正在关闭蓝牙的回调
 */
@Override
public void onBluetoothStateTurningOff() {
    // TODO
}

/**
 * 蓝牙关闭的回调
 */
@Override
public void onBluetoothStateOff() {
    // TODO
}

/**
 * 正在打开蓝牙的回调
 */
@Override
public void onBluetoothStateTurningOn() {
    // TODO
}

/**
 * 蓝牙打开的回调
 */
@Override
public void onBluetoothStateOn() {
    // TODO
}

移除蓝牙开关状态的监听

mBluetoothManager.removeOnBluetoothStateListener();

设置蓝牙可见

startActivity(mBluetoothManager.getDurationIntent(0));

获取蓝牙名称

mBluetoothManager.getName()

修改蓝牙名称

mBluetoothManager.setName(newName);

扫描附近的蓝牙设备

mBluetoothManager.discovery();

添加扫描蓝牙设备的监听

mBluetoothManager.setOnDiscoveryDeviceListener(this);
/**
 * 开始扫描附近蓝牙设备的回调
 */
@Override
public void onDiscoveryDeviceStarted() {
    // TODO
}

/**
 * 扫描到附近蓝牙设备的回调
 *
 * @param device 蓝牙设备
 */
@Override
public void onDiscoveryDeviceFound(BluetoothDevice device) {
    // TODO
}

/**
 * 扫描附近蓝牙设备完成的回调
 */
@Override
public void onDiscoveryDeviceFinished() {
    // TODO
}

移除扫描蓝牙设备的监听

mBluetoothManager.removeOnDiscoveryDeviceListener();

服务端

初始化

mBluetoothService = new BluetoothService() {

    @Override
    protected UUID onSecureUuid() {
        // TODO 设置自己的UUID
        return UUID_SECURE;
    }

    @Override
    protected UUID onInsecureUuid() {
        // TODO 设置自己的UUID
        return UUID_INSECURE;
    }
};

等待客户端连接

mBluetoothService.start();

断开连接/释放资源

mBluetoothService.stop();

添加蓝牙连接的监听

mBluetoothService.setOnServiceConnectListener(new OnServiceConnectListener() {
    @Override
    public void onConnectListening() {
        // TODO
    }

    @Override
    public void onConnectSuccess(BluetoothDevice device) {
        // TODO
    }

    @Override
    public void onConnectFail(Exception e) {
        // TODO
    }

    @Override
    public void onConnectLost(Exception e) {
        // TODO
    }
});

发送消息

mBluetoothService.send(chatText);

添加消息收发的监听

mBluetoothClient.setOnMessageListener(this);
/**
 * 蓝牙发送了消息
 *
 * @param message 发送的消息
 */
@Override
public void onSend(String message) {
    // TODO
}

/**
 * 蓝牙接收到消息
 *
 * @param message 接收的消息
 */
@Override
public void onRead(String message) {
    // TODO
}

客户端

初始化

mBluetoothClient = new BluetoothClient() {
    @Override
    protected UUID onSecureUuid() {
        // TODO 设置自己的UUID
        return UUID_SECURE;
    }

    @Override
    protected UUID onInsecureUuid() {
        // TODO 设置自己的UUID
        return UUID_INSECURE;
    }
};

蓝牙连接(安全)

mBluetoothClient.connect(mBluetoothDevice, true);

蓝牙连接(不安全)

mBluetoothClient.connect(mBluetoothDevice, false);

断开连接/释放资源

mBluetoothClient.stop();

添加蓝牙连接的监听

mBluetoothClient.setOnClientConnectListener(new OnClientConnectListener() {
    @Override
    public void onConnecting() {
        // TODO
    }

    @Override
    public void onConnectSuccess(BluetoothDevice device) {
        // TODO
    }

    @Override
    public void onConnectFail(Exception e) {
        // TODO
    }

    @Override
    public void onConnectLost(Exception e) {
        // TODO
    }
});

发送消息

mBluetoothClient.send(chatText);

添加消息收发的监听

mBluetoothClient.setOnMessageListener(this);
/**
 * 蓝牙发送了消息
 *
 * @param message 发送的消息
 */
@Override
public void onSend(String message) {
    // TODO
}

/**
 * 蓝牙接收到消息
 *
 * @param message 接收的消息
 */
@Override
public void onRead(String message) {
    // TODO
}
  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值