查找 Android 蓝牙配对设备

发布于 2024-12-01 12:16:41 字数 996 浏览 0 评论 0原文

我正在尝试创建一个图像按钮,按下该按钮时,会向用户显示要连接的配对蓝牙设备的列表。

但是,我在 ##1 处得到“Set无法解析为变量”, 和“mArrayAdapber 无法解析”在点 ##2 (##1和##2不是代码的一部分...)

我使用了Android站点的代码,但是在黑暗中,我发现自己处于黑暗中。

我希望得到一些指导...

//搜索

ImageButton bSearch = (ImageButton) findViewById(R.id.Search);
bSearch.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {                        
        ##1Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        // If there are paired devices
        if (pairedDevices.size() > 0) {
            // Loop through paired devices
            for (BluetoothDevice device : pairedDevices) {
                // Add the name and address to an array adapter to show in a ListView
                ##2mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        }                                                                           
    }                       
});

I'm trying to create an image button that, when pressed, presents the users a list of Paired Bluetooth devices to connect to.

However, I get "Set cannot be resolved as a variable" at point ##1,
and "mArrayAdapber cannot be resolved" at point ##2
(##1 and ##2 are not part of the code...)

I used the code from the Android site, but being in the dark, I find myself in the dark.

I'd appreciate some guidance...

//Search

ImageButton bSearch = (ImageButton) findViewById(R.id.Search);
bSearch.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {                        
        ##1Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        // If there are paired devices
        if (pairedDevices.size() > 0) {
            // Loop through paired devices
            for (BluetoothDevice device : pairedDevices) {
                // Add the name and address to an array adapter to show in a ListView
                ##2mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        }                                                                           
    }                       
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最终幸福 2024-12-08 12:16:41

对于 1) 如果您还没有这样做,请添加

> 导入 java.util.Set;

在导入语句中 。这将解决“设置”错误。

对于2)声明并初始化

mArrayAdapter

例如在您的 Activity 中执行以下操作:

private ArrayAdapter<String> mArrayAdapter;

然后在 onCreate: 上执行操作,

 mArrayAdapter= new ArrayAdapter<String>(this, <your layout file>);

然后将其添加到 ListView 中

// 为新发现的设备查找并设置 ListView

   ListView newDevicesListView = (ListView)
 findViewById(R.id.<layout_file>);
         newDevicesListView.setAdapter(mArrayAdapter);

 newDevicesListView.setOnItemClickListener(mDeviceClickListener);

请参阅 Android 示例中的蓝牙聊天示例。它应该可以帮助您开始使用蓝牙 API 的


评论更新:

如果您仔细查看 BT 示例中的 BluetoothChat.java 文件,您将看到此

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(D) Log.d(TAG, "onActivityResult " + resultCode);
        switch (requestCode) {
        case REQUEST_CONNECT_DEVICE:
            // When DeviceListActivity returns with a device to connect
            if (resultCode == Activity.RESULT_OK) {
                // Get the device MAC address
                String address = data.getExtras()
                                     .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                // Get the BLuetoothDevice object
                BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
                // Attempt to connect to the device
                mChatService.connect(device);
            }
            break;
        case REQUEST_ENABLE_BT:
            // When the request to enable Bluetooth returns
            if (resultCode == Activity.RESULT_OK) {
                // Bluetooth is now enabled, so set up a chat session
                setupChat();
            } else {
                // User did not enable Bluetooth or an error occured
                Log.d(TAG, "BT not enabled");
                Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

观看这一行:

 // Attempt to connect to the device
 mChatService.connect(device);

此函数连接至蓝牙设备。第一次它会要求您自动配对。配对后,下次它将自动连接到蓝牙设备。

For 1) Well if you haven't done so , add

> import java.util.Set;

in your import statements . This will resolve "Set" error.

For 2) Declare and initialize

mArrayAdapter

For example in your Activity do :

private ArrayAdapter<String> mArrayAdapter;

and then on onCreate:

 mArrayAdapter= new ArrayAdapter<String>(this, <your layout file>);

which should then be added to a ListView

// Find and set up the ListView for newly discovered devices

   ListView newDevicesListView = (ListView)
 findViewById(R.id.<layout_file>);
         newDevicesListView.setAdapter(mArrayAdapter);

 newDevicesListView.setOnItemClickListener(mDeviceClickListener);

Refer to Bluetooth Chat example from Android examples. It should help you get going with the Bluetooth api's


Update on comment :

If you look closely on BluetoothChat.java file in BT example, you'll see this

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(D) Log.d(TAG, "onActivityResult " + resultCode);
        switch (requestCode) {
        case REQUEST_CONNECT_DEVICE:
            // When DeviceListActivity returns with a device to connect
            if (resultCode == Activity.RESULT_OK) {
                // Get the device MAC address
                String address = data.getExtras()
                                     .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                // Get the BLuetoothDevice object
                BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
                // Attempt to connect to the device
                mChatService.connect(device);
            }
            break;
        case REQUEST_ENABLE_BT:
            // When the request to enable Bluetooth returns
            if (resultCode == Activity.RESULT_OK) {
                // Bluetooth is now enabled, so set up a chat session
                setupChat();
            } else {
                // User did not enable Bluetooth or an error occured
                Log.d(TAG, "BT not enabled");
                Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

Watch this line :

 // Attempt to connect to the device
 mChatService.connect(device);

This function connects to bluetooth device. First time it'll ask you to pair it automatically. Once paired, next time it'll auto connect to the bluetooth device.

噩梦成真你也成魔 2024-12-08 12:16:41

嗨,您也可以在刚刚获得绑定设备集的情况下尝试此代码。

 private ArrayAdapter<String> bondedAdapter = null;
    private ListView listViewPairedDevices = null;

@Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        try {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    listViewPairedDevices = (ListView) findViewById(R.id.listViewPairedDevices);
    bondedAdapter = new ArrayAdapter<String>(this, R.layout.lyt_scanlist_textview);

    Set<BluetoothDevice> bondedSet = bluetoothAdapter.getBondedDevices();
                Log.v(BluetoothDemoActivity.LOG, "BluetoothDemo : bondedSet: "+bondedSet);

                int count = 0;
                if(bondedSet.size() > 0){
                    for(BluetoothDevice device : bondedSet){
                        textViewPairedDevice.setVisibility(View.VISIBLE);
                        bondedAdapter.add(device.getName()+"\n"+device.getAddress());
                        Log.v(BluetoothDemoActivity.LOG, " count = "+count++);
                    }
                }else{
                    bondedAdapter.add("No Devices");
                }

    listViewPairedDevices.setAdapter(bondedAdapter);
} catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e(BluetoothDemoActivity.LOG, e.toString(),e.fillInStackTrace());
        }
    }//onStart ends 

Hi, You can also try this code where you just have get the Set of bonded devices.

 private ArrayAdapter<String> bondedAdapter = null;
    private ListView listViewPairedDevices = null;

@Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        try {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    listViewPairedDevices = (ListView) findViewById(R.id.listViewPairedDevices);
    bondedAdapter = new ArrayAdapter<String>(this, R.layout.lyt_scanlist_textview);

    Set<BluetoothDevice> bondedSet = bluetoothAdapter.getBondedDevices();
                Log.v(BluetoothDemoActivity.LOG, "BluetoothDemo : bondedSet: "+bondedSet);

                int count = 0;
                if(bondedSet.size() > 0){
                    for(BluetoothDevice device : bondedSet){
                        textViewPairedDevice.setVisibility(View.VISIBLE);
                        bondedAdapter.add(device.getName()+"\n"+device.getAddress());
                        Log.v(BluetoothDemoActivity.LOG, " count = "+count++);
                    }
                }else{
                    bondedAdapter.add("No Devices");
                }

    listViewPairedDevices.setAdapter(bondedAdapter);
} catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e(BluetoothDemoActivity.LOG, e.toString(),e.fillInStackTrace());
        }
    }//onStart ends 
掐死时间 2024-12-08 12:16:41

只需分别从 ##1Set##2mArrayAdapter 中删除 ##1##2 即可你的代码。我认为您只是从其他来源复制/粘贴而没有注意。这不是原始代码的一部分。它仅用于列表编号目的。

Just remove ##1 and ##2 respectively from ##1Set<BluetoothDevice> and ##2mArrayAdapter on your code. I think you'd just copy/paste from another source and did not pay attention to. That is not part of the original code. It just used for list number purposes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文