bluetooth 第一次开机启动并实现自动配对

来源:互联网 发布:mac怎么下载skype 编辑:程序博客网 时间:2024/06/12 21:59

第一次开机自动启动:

对于ICSICS2JB版本,将

frameworks/base/packages/SettingsProvider/res/values/default.xml下面

def_bluetooth_on改成true

   <bool name="def_bluetooth_on">false</bool>

改为:

   <bool name="def_bluetooth_on">true</bool>

 

对于JB2JB3,JB5打过PATCH ALPS00574277的版本,则还需要在

frameworks/base/services/java/com/android/server/systemserver.java

中加如下代码:

if (true == FeatureOption.MTK_BT_PROFILE_MANAGER) {

BluetoothProfileManager = new BluetoothProfileManagerService(context);

ServiceManager.addService(BluetoothProfileManagerService.BLUETOOTH_PROFILEMANAGER_SERVICE,BluetoothProfileManager);

}

 

//add_for_bt_auto_open_at_first_time+++

int bluetoothState = Settings.Global.getInt(mContentResolver,

Settings.Global.BLUETOOTH_ON, 0);

Slog.i(TAG, "bluetoothState = " + bluetoothState);

if (bluetoothState != 0) {

    bluetooth.enable();

}

 

//add_for_bt_auto_open_at_first_time---

/// M: ALPS00574277 @{

boolean bluetoothOn = bluetooth.getRebootBluetoothState();

Slog.i(TAG, "bluetoothOn = " + bluetoothOn);

if (bluetoothOn) {

    bluetooth.enable();

}

 

 

如何在Framework层实现BT的自动配对

在项目需要时,Bluetooth能自动配对,不需要弹出需要用户确认的对话框。

Android手机平台上当对方发起配对时主要使用2种配对方式:

1.SSP数字比对,即双方手机产生配对密钥,由用户选择yes or no来进行鉴权连接。

2.Pin Code, 如果对方蓝牙不支持BT2.1以上版本,一般会走此流程,即双方输入四位数字进行配对。此方式需要预置四位数字,当对方发起配对时,需要输入此四位数字。

 

下面的内容是如何添加代码来实现BT自动配对的流程:

修改文件:

packages\apps\settings\src\com\android\settings\bluetooth\BluetoothPairingRequest.java

总共修改5处。

1.import android.util.Log;

2.BluetoothPairingRequest类中定义两个变量

         private static int mType = 0;

         private BluetoothDevice mDevice;

3.BluetoothPairingRequest类中添加私有方法

   private void autoPair(int value) {

       switch (mType) {

           case BluetoothDevice.PAIRING_VARIANT_PIN:

                   byte[] pin= {0,1,2,3};

                   mDevice.setPin(pin);

                   break;

           case BluetoothDevice.PAIRING_VARIANT_PASSKEY:

                   mDevice.setPasskey(value);

                   break;

           case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:

           case BluetoothDevice.PAIRING_VARIANT_CONSENT:

                   mDevice.setPairingConfirmation(true);

                   break;

           case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:

           case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:

                   // Do nothing.

                    break;

           case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:

                   mDevice.setRemoteOutOfBandData();

                    break;

           default:

                   Log.e("autoPair", "Incorrect pairing type received");

       }

   }

4.  int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,

                   BluetoothDevice.ERROR);

        后面添加以下两句

          mType = type;

          mDevice = device;

5. 注释掉context.startActivity(pairingIntent);这句,

并在 if (powerManager.isScreenOn() &&前面添加

    int pairingKey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY,

        BluetoothDevice.ERROR);

    autoPair(pairingKey);

阅读全文
0 0
原创粉丝点击