【问题标题】:To discover and pair Bluetooth Devices发现和配对蓝牙设备
【发布时间】:2011-07-13 06:58:40
【问题描述】:

如何使用 Java 发现和配对 Android 蓝牙设备?有什么代码可以参考吗?

【问题讨论】:

    标签: java android eclipse bluetooth device-name


    【解决方案1】:

    下面的代码将发现配对和未配对的设备列表,然后你必须实现客户端和服务器,它负责配对设备并将数据发送到设备,因为你可以使用@ 987654321@这会给你一个想法。

    private Set<BluetoothDevice> pairedDevices;
    public static ArrayList<Object> BondedDeviceList;
    public static ArrayList<Object> NewDeviceList;
    
     public void makeDiscoverable()
    {
        discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        activity.startActivity(discoverableIntent);
    }
    
    //It will Add the paired device in the BondedDeviceList
    public void queryPairedDevice(){
        pairedDevices = mBluetoothAdapter.getBondedDevices();
    
        // If there are paired devices
        if(pairedDevices==null)
        {
            //No Bonded Devices 
    
        }else
        {
            if (pairedDevices.size() > 0) {
                // Loop through paired devices
                for (BluetoothDevice device : pairedDevices) {
                    BondedDeviceList.add(device);
                }
                BondedDeviceList.add("End");
            }
        }
    }
    
    //Broadcast Receiver will find the Available devices and the discovery finished
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
    
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action.trim())) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // If it's already paired, skip it, because it's been listed already
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    NewDeviceList.add(device);
                }
                // When discovery is finished, change the Activity title
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                if (NewDeviceList.isEmpty() == true) {
                    String noDevices = "No Devices";
                    NewDeviceList.add(noDevices);
                }
                System.out.println("Discovery Finished!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                NewDeviceList.add("End");
            }
        }
    };
    
    //This is query for the bluetooth devices 
    public void queryDevices(){
        actionFoundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        activity.registerReceiver(mReceiver, actionFoundFilter);
        // Don't forget to unregister during onDestroy
    
        discoveryFinishedFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        activity.registerReceiver(mReceiver, discoveryFinishedFilter); 
        // Don't forget to unregister during onDestroy
        queryPairedDevice();
        mBluetoothAdapter.startDiscovery();
    }
    
    
    //Unregister the receivers
    public void unregisterReceiver() {
        // Make sure we're not doing discovery anymore
        if (mBluetoothAdapter != null) {
                mBluetoothAdapter.cancelDiscovery();
        }
        // Unregister broadcast listeners
        activity.unregisterReceiver(mReceiver);
    }
    

    干杯。

    【讨论】:

    • 谢谢侯赛因!但是 onCreate 部分我不知道如何编码。你有任何示例吗?此外,客户端和服务器活动我不确定应该包含哪些内容..
    • 只要你想从你的活动中,根据你的要求调用可用的函数,去那里的BluetoothChatSample,客户端/服务器部分解释清楚.. :)
    猜你喜欢
    • 2012-04-19
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多