蓝牙接收器无法正常工作

时间:2016-07-03 20:23:20

标签: android android-bluetooth

我看了很多例子,一切似乎都是正确的。我有两个权限,检查它们是否被激活。我看过Stackoverflow上的无数帖子。我知道这是一个非常小的东西,我总是喜欢。我确保我搜索的所有设备都可见。我甚至用系统蓝牙进行了双重检查,发现它只是找到了。我究竟做错了什么。这是我的代码。

public class Bluetooth_ListView extends ListActivity
{
    private BluetoothAdapter mBluetoothAdapter;
    private ArrayList<Bluetooth> arrayOfFoundBTDevices;
    private BroadcastReceiver mReceiver;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // Quick permission check
        int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
        permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
        if (permissionCheck != 0) {

            this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001);
        }
        if (mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
        }
        mBluetoothAdapter.startDiscovery();

        displayListOfFoundDevices();
    }

    private void displayListOfFoundDevices()
    {
        arrayOfFoundBTDevices = new ArrayList<Bluetooth>();


        // Create a BroadcastReceiver for ACTION_FOUND
        mReceiver = new BroadcastReceiver()
        {

            @Override
            public void onReceive(Context context, Intent intent)
            {
                Log.v("discover_me", "Bluetooth receiving");
                String action = intent.getAction();
                // When discovery finds a device
                if (BluetoothDevice.ACTION_FOUND.equals(action))
                {
                    // Get the bluetoothDevice object from the Intent
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                    int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);

                    // Create the device object and add it to the arrayList of devices
                    Bluetooth bluetoothObject = new Bluetooth();
                    bluetoothObject.setBluetooth_name(device.getName());
                    bluetoothObject.setBluetooth_address(device.getAddress());
                    bluetoothObject.setBluetooth_state(device.getBondState());
                    bluetoothObject.setBluetooth_type(device.getType());    
                    bluetoothObject.setBluetooth_uuids(device.getUuids());
                    bluetoothObject.setBluetooth_rssi(rssi);

                    arrayOfFoundBTDevices.add(bluetoothObject);

                    Bluetooth_Adapter adapter = new Bluetooth_Adapter(getApplicationContext(), arrayOfFoundBTDevices);

                    setListAdapter(adapter);
                }
            }
        };
        // Register the BroadcastReceiver
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        mBluetoothAdapter.cancelDiscovery();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        unregisterReceiver(mReceiver);
        mBluetoothAdapter.cancelDiscovery();
    }
}

任何建议都很棒。无论如何,阵列总是空白的。此外,onReceive中的Log.v永远不会被调用。

1 个答案:

答案 0 :(得分:0)

评论太多了。所以我决定把它作为答案。

您需要将蓝牙设备可发现,以便在附近找到的蓝牙设备列表中显示它们。

如果这是BT耳机,那么必须采用某种机制来强制BT耳机被发现。现在大多数BT耳机默认不可发现。我遇到了问题,我在SO中将其作为question发布。

您也可以看到此answer。虽然答案不合适,但我想分享我的发现。希望这会有所帮助。