AndroidThings蓝牙问题处理

Android things系统中并没有提供setting application,所以所有的蓝牙处理需要我们自行维护,故如果我们需要使用蓝牙连接必须使用Android 自己去做扫描,配对,连接整个过程

比如:

蓝牙配对


解除配对


以及设置pin,连接等,都需要调用大量的反射,在这里我给大家推荐的方法是:

使用things sdk中存在的BluetoothConnectionManager类,通过查阅文档得知,google为了做things上的蓝牙适配,特此对外公布了此接口

蓝牙配对示例:

import android.bluetooth.BluetoothDevice;
import com.google.android.things.bluetooth.BluetoothConnectionManager;
import com.google.android.things.bluetooth.BluetoothPairingCallback;
import com.google.android.things.bluetooth.PairingParams;
...

public class PairingActivity extends Activity {

    BluetoothConnectionManager mBluetoothConnectionManager;

    @Override
    protected void onCreate(Bundled savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBluetoothConnectionManager = BluetoothConnectionManager.getInstance();
        mBluetoothConnectionManager.registerPairingCallback(mBluetoothPairingCallback);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBluetoothConnectionManager.unregisterPairingCallback(mBluetoothPairingCallback);
    }

    private void startPairing(BluetoothDevice remoteDevice) {
        mBluetoothConnectionManager.initiatePairing(remoteDevice);
    }

    private BluetoothPairingCallback mBluetoothPairingCallback = new BluetoothPairingCallback() {

        @Override
        public void onPairingInitiated(BluetoothDevice bluetoothDevice,
                PairingParams pairingParams) {
            // Handle incoming pairing request or confirmation of outgoing pairing request
            handlePairingRequest(bluetoothDevice, pairingParams);
        }

        @Override
        public void onPaired(BluetoothDevice bluetoothDevice) {
            // Device pairing complete
        }

        @Override
        public void onUnpaired(BluetoothDevice bluetoothDevice) {
            // Device unpaired
        }

        @Override
        public void onPairingError(BluetoothDevice bluetoothDevice,
                BluetoothPairingCallback.PairingError pairingError) {
            // Something went wrong!
        }
    };
}

蓝牙连接示例:

import android.bluetooth.BluetoothDevice;
import com.google.android.things.bluetooth.BluetoothConnectionManager;
import com.google.android.things.bluetooth.BluetoothConnectionCallback;
import com.google.android.things.bluetooth.BluetoothProfile;
import com.google.android.things.bluetooth.ConnectionParams;
...

public class ConnectActivity extends Activity {

    BluetoothConnectionManager mBluetoothConnectionManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBluetoothConnectionManager = BluetoothConnectionManager.getInstance();
        mBluetoothConnectionManager.registerConnectionCallback(mBluetoothConnectionCallback);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBluetoothConnectionManager.unregisterConnectionCallback(mBluetoothConnectionCallback);
    }

    private void connectToA2dp(BluetoothDevice bluetoothDevice) {
        mBluetoothConnectionManager.connect(bluetoothDevice, BluetoothProfile.A2DP_SINK);
    }

    // Set up callbacks for the profile connection process.
    private final BluetoothConnectionCallback mBluetoothConnectionCallback = new BluetoothConnectionCallback() {
        @Override
        public void onConnectionRequested(BluetoothDevice bluetoothDevice, ConnectionParams connectionParams) {
            // Handle incoming connection request
            handleConnectionRequest();
        }

        @Override
        public void onConnectionRequestCancelled(BluetoothDevice bluetoothDevice, int requestType) {
            // Request cancelled
        }

        @Override
        public void onConnected(BluetoothDevice bluetoothDevice, int profile) {
            // Connection completed successfully
        }

        @Override
        public void onDisconnected(BluetoothDevice bluetoothDevice, int profile) {
            // Remote device disconnected
        }
    };
}

官方文档请查阅

https://developer.android.google.cn/things/sdk/apis/bluetooth.html

-------------------------------------------------------------------------

注意:NXP Pico i.MX7D请务必插上天线再进行调试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值