【问题标题】:Bluetooth Receiver not working蓝牙接收器不工作
【发布时间】:2016-07-03 20:23:19
【问题描述】:

我看过很多例子,一切似乎都是正确的。我有这两个权限,检查它们是否被激活。我看过无数关于 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 永远不会被调用。

【问题讨论】:

  • 还有,问题是什么? onReceive 方法永远不会被调用或者结果是一个空数组?另一个问题:日志记录功能 Log.v 是否有效?我记得有时当我使用 android 时,我在使用 Log 功能时遇到了一些问题。
  • 它总是返回一个空白数组。 onReceive 中的 Log.v 永远不会被调用。我旁边有 4 个蓝牙设备,但没有找到。它似乎从未检测到或接收来自任何设备的任何信息。
  • 如果你首先调用 displayListOfFoundDevices() 并开始发现过程呢?
  • 我试过了。我尝试只调用 displayListOfFoundDevices () 并在函数内部调用 start discovery,但仍然没有。
  • 但是您不能在函数内部开始发现,因为如果发现过程找到任何活动的蓝牙设备,就会调用该函数。只需尝试交换订单以首先调用 displayListOfFoundDevices();然后是 mBluetoothAdapter.startDiscovery();

标签: android android-bluetooth


【解决方案1】:

cmets 太多。所以我决定把它作为答案。

您需要将您的蓝牙设备设置为可发现,以使其在您附近找到的蓝牙设备列表中可见。

如果这是一个 BT 耳机,那么必须有某种机制来强制 BT 耳机变得可发现。现在默认情况下无法发现大多数 BT 耳机。我有一个问题,我在 SO 中将其发布为question。

你也可以看到这个answer。虽然答案不是正确的,但我想分享我的发现。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多