Java 查找和配对蓝牙设备

Java 查找和配对蓝牙设备,java,android,eclipse,bluetooth,device-name,Java,Android,Eclipse,Bluetooth,Device Name,如何使用Java发现和配对Android蓝牙设备?有什么代码供我参考吗?下面的代码将发现配对和未配对设备的列表,然后你必须实现客户端和服务器,它负责配对设备并向设备发送数据,因为你可以利用它来给你一个想法 private Set<BluetoothDevice> pairedDevices; public static ArrayList<Object> BondedDeviceList; public static ArrayList<Object> New

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

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

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);
}
私有集对设备;
公共静态数组列表BondedDeviceList;
公共静态数组列表NewDeviceList;
公共无效使可发现()
{
discoverableIntent=新意图(BluetoothAdapter.ACTION\u REQUEST\u DISCOVERABLE);
可发现意图.putExtra(BluetoothAdapter.EXTRA可发现持续时间,300);
活动。开始触觉(发现意图);
}
//它将在BondedDeviceList中添加配对设备
public void queryPairedDevice(){
pairedDevices=mBluetoothAdapter.getBondedDevices();
//如果有配对的设备
if(pairedDevices==null)
{
//无粘结装置
}否则
{
如果(pairedDevices.size()>0){
//循环通过配对设备
用于(蓝牙设备:pairedDevices){
BondedDeviceList.add(设备);
}
BondedDeviceList.添加(“结束”);
}
}
}
//广播接收器将找到可用的设备并完成查找
专用最终广播接收器mReceiver=新广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
//当发现发现发现设备时
if(BluetoothDevice.ACTION\u FOUND.equals(ACTION.trim())){
//从Intent获取BluetoothDevice对象
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
//如果它已配对,请跳过它,因为它已列出
if(device.getBondState()!=BluetoothDevice.BOND\u BOND){
添加(设备);
}
//发现完成后,更改活动标题
}else if(BluetoothAdapter.ACTION\u DISCOVERY\u FINISHED.equals(ACTION)){
if(NewDeviceList.isEmpty()==true){
String noDevices=“无设备”;
添加(节点设备);
}
System.out.println(“发现完成!!!!!!!!!!!!!!!!!!!!!!!!!!!”;
新增设备列表。添加(“结束”);
}
}
};
//这是对蓝牙设备的查询
公共无效查询设备(){
actionFoundFilter=新的IntentFilter(BluetoothDevice.ACTION\u已找到);
activity.registerReceiver(mrReceiver、actionFoundFilter);
//在onDestroy期间不要忘记注销
discoveryFinishedFilter=新的意向过滤器(BluetoothAdapter.ACTION\u DISCOVERY\u FINISHED);
活动。注册接收者(mReceiver,discoveryFinishedFilter);
//在onDestroy期间不要忘记注销
queryPairedDevice();
mBluetoothAdapter.startDiscovery();
}
//注销接收人的注册
公共无效取消注册接收人(){
//确保我们不再进行探索
if(mBluetoothAdapter!=null){
mBluetoothAdapter.cancelDiscovery();
}
//取消注册广播侦听器
活动。未注册接收人(mReceiver);
}

干杯。

谢谢你,侯赛因!但是onCreate部分我不知道如何编码。。有样品吗?此外,客户机和服务器活动我不确定应该包含哪些内容..根据您的需要随时从您的活动中调用可用的函数,通过BluetoothChatSample,客户机/服务器部分已清楚解释..)