Android 藍芽通訊——AndroidBluetoothManager

來源:互聯網
上載者:User

標籤:file   text   word   public   src   androi   func   github   ade   

轉載請說明出處!
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

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcTQ4Nzg4MDI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="這裡寫圖片描寫敘述" title="">

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);
/** * 正在關閉藍芽的回調 */@Overridepublic void onBluetoothStateTurningOff() {    // TODO}/** * 藍芽關閉的回調 */@Overridepublic void onBluetoothStateOff() {    // TODO}/** * 正在開啟藍芽的回調 */@Overridepublic void onBluetoothStateTurningOn() {    // TODO}/** * 藍芽開啟的回調 */@Overridepublic void onBluetoothStateOn() {    // TODO}
移除藍芽開關狀態的監聽
mBluetoothManager.removeOnBluetoothStateListener();
設定藍芽可見
startActivity(mBluetoothManager.getDurationIntent(0));
擷取藍芽名稱
mBluetoothManager.getName()
改動藍芽名稱
mBluetoothManager.setName(newName);
掃描附近的藍牙裝置
mBluetoothManager.discovery();
加入掃描藍牙裝置的監聽
mBluetoothManager.setOnDiscoveryDeviceListener(this);
/** * 開始掃描附近藍牙裝置的回調 */@Overridepublic void onDiscoveryDeviceStarted() {    // TODO}/** * 掃描到附近藍牙裝置的回調 * * @param device 藍牙裝置 */@Overridepublic void onDiscoveryDeviceFound(BluetoothDevice device) {    // TODO}/** * 掃描附近藍牙裝置完畢的回調 */@Overridepublic 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;    }};
等待client串連
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 發送的訊息 */@Overridepublic void onSend(String message) {    // TODO}/** * 藍芽接收到訊息 * * @param message 接收的訊息 */@Overridepublic void onRead(String message) {    // TODO}
client初始化
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 發送的訊息 */@Overridepublic void onSend(String message) {    // TODO}/** * 藍芽接收到訊息 * * @param message 接收的訊息 */@Overridepublic void onRead(String message) {    // TODO}

Android 藍芽通訊——AndroidBluetoothManager

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.