Подключение BluetoothTV пульта AndroidTV - PullRequest
0 голосов
/ 20 сентября 2019

У меня проблемы при попытке подключения удаленного контроллера Bluetooth.Соединение с устройством установлено, но удаленные команды не работают.

Кто-нибудь знает, что я делаю не так?

Шаги:

  1. проверьте, включен ли Bluetooth, и запустите, если его нет
  2. Запустите Bluetoothобнаружение
  3. на устройстве, обнаруженном с определенным mac-адресом, попробуйте выполнить сопряжение
  4. , отмените обнаружение и создайте связь
  5. на устройстве, попытайтесь установить соединение и установить автоматическое соединение
package com.example.setuphooktest;

import
...

public class HookActivity extends Activity {

    private static final String TAG = "CustomSetup";
    private static final int REQUEST_ENABLE_BT = 1233211323;

    BluetoothAdapter mBluetoothAdapter;
    BluetoothLeScanner mBluetoothLeScanner;
    Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hook);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        registerReceiver(mReceiver, filter);

        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }

        mBluetoothAdapter.startDiscovery()
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            switch (action) {
                case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
                    if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                        connectDevice(device);
                        break;
                    }
                    break;
                case BluetoothDevice.ACTION_FOUND:
                    String deviceHardwareAddress = device.getAddress();

                    if (deviceHardwareAddress.substring(0, 8).equalsIgnoreCase("12:34:56")) {
                        pairDevice(device);
                    }

                    break;

                case BluetoothDevice.ACTION_ACL_CONNECTED:
                    mBluetoothAdapter.cancelDiscovery();

                    break;
                case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                    break;
            }
        }
    };

    private void connectDevice(BluetoothDevice device) {
        device.connectGatt(this, true, new BluetoothGattCallback() {});
    }

    private void pairDevice(BluetoothDevice device) {
        try {
            mBluetoothAdapter.cancelDiscovery();
            device.createBond();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    @Override
    protected void onDestroy() {
       ...
    }
}
...