蓝牙不搜索新设备/如何通过蓝牙发送字符串消息?

时间:2013-12-09 12:33:56

标签: android bluetooth

我在我的应用程序中使用蓝牙,我希望我的应用程序搜索新设备并与它们配对,然后向他们发送预定义消息,这是我到目前为止所做的:

1-这是初始化我的蓝牙变量的功能

public void BlutoothInit()

{

BlutoothListView=(ListView)findViewById(R.id.minimal_profile);

pairedDevices = new ArrayList<String>();
    filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    receiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                devices.add(device);
                String s = "";
                for(int a = 0; a < pairedDevices.size(); a++){
                    if(device.getName().equals(pairedDevices.get(a))){
                        s = "(Paired)";
                        break;
                    }
                }

                listAdapter.add(device.getName()+" "+s+" "+"\n"+device.getAddress());
            }

            else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
            }
            else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){         
            }
            else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
                if(btAdapter.getState() == btAdapter.STATE_OFF){
                    //turnOnBT();
                }
            }

        }
    };
        registerReceiver(receiver, filter);
         filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        registerReceiver(receiver, filter);
         filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(receiver, filter);
         filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(receiver, filter);
}

2-她正在使用蓝牙

public void via_bluetooth (View v) {
getPairedDevices();


final ListView DevicesListView = (ListView) findViewById(R.id.minimal_profile);

ArrayAdapter<String> adapter;

adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,DevicesList);

DevicesListView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    DevicesListView.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
                Toast.makeText(getApplicationContext(), "device is not paired", 0).show();
            return true;
        }                 
      });

}

函数“getPairedDevices”工作正常,我从我的应用程序外部获取所有与之配对的设备 但是当我调用此功能来扫描新设备时

public void ScanNewDevices(View v) {
    // TODO Auto-generated method stub
    if (btAdapter.isDiscovering()) {
        btAdapter.cancelDiscovery();
    }   
    btAdapter.startDiscovery();
    try{
    final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            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);
                // Add the name and address to an array adapter to show in a ListView
                DevicesList.add(device.getName() + "\n" + device.getAddress());
            }
        }
    };
    // Register the BroadcastReceiver
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
    }
     catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.toString(), 0).show();
         }
}

错误显示“未找到来源”。出现什么都不做! 我们做错了什么以及搜索设备并向他们发送消息时缺少什么?

1 个答案:

答案 0 :(得分:0)

 private void pairDevice(BluetoothDevice device) {
    try {

        Method m = device.getClass().getMethod("createBond", (Class[]) null);
        m.invoke(device, (Object[]) null);

        Toast.makeText(context,"paired finished",3000).show();
        } catch (Exception e) 
          {
        Toast.makeText(context,"error"+e.getMessage(),3000).show();

    }
    }
      //try this for paring...