蓝牙插座连接

时间:2010-11-11 20:13:45

标签: android bluetooth

我正在创建一个使用android通过蓝牙发送和接收数据的应用程序。但是我在创建套接字时遇到了问题。他陷入了那条线mmSocket btserver.accept =();我不能与任何设备配对。

我有另一个疑问,我可以用android和symbian进行交流吗?

1 个答案:

答案 0 :(得分:1)

代码如下。他找到设备但没有打开通信插座。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    bluetooth = BluetoothAdapter.getDefaultAdapter();
    msg = (TextView) findViewById(R.id.msg);

    if (bluetooth.isEnabled()) {
       String address = bluetooth.getAddress();
       String name = bluetooth.getName();
       toastText = name + " : " + address;
    }
    else
       toastText = "Bluetooth is not enabled";

    Toast.makeText(this, toastText, Toast.LENGTH_LONG).show();
    msg.append("Bluetooth is not enabled");

    enableBT = BluetoothAdapter.ACTION_REQUEST_ENABLE;
    startActivityForResult(new Intent(enableBT), 0);

    String aDiscoverable = BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
    startActivityForResult(new Intent(aDiscoverable),DISCOVERY_REQUEST);

    registerReceiver(new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String prevScanMode = BluetoothAdapter.EXTRA_PREVIOUS_SCAN_MODE;
            String scanMode = BluetoothAdapter.EXTRA_SCAN_MODE;
            int scanMode1 = intent.getIntExtra(scanMode, -1);
            int prevMode1 = intent.getIntExtra(prevScanMode, -1);
        }
    },
    new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED));

    BroadcastReceiver discoveryResult = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            remoteDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
            remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Toast.makeText(getApplicationContext(),
            "Discovered: " + remoteDeviceName,
            Toast.LENGTH_SHORT).show();
            msg.append("\n"+remoteDeviceName);
            msg.append("\n"+remoteDevice);
     }
    };

    BroadcastReceiver discoveryMonitor = new BroadcastReceiver() {
        String dStarted = BluetoothAdapter.ACTION_DISCOVERY_STARTED;
        String dFinished = BluetoothAdapter.ACTION_DISCOVERY_FINISHED;

        public void onReceive(Context context, Intent intent) {
           if (dStarted.equals(intent.getAction())) {

              Toast.makeText(getApplicationContext(),
              "Discovery Started . . . ", Toast.LENGTH_SHORT).show();
          }
          else if (dFinished.equals(intent.getAction())) {
             Toast.makeText(getApplicationContext(),
            "Discovery Completed . . . ", Toast.LENGTH_SHORT).show();
         }
       }
    };
    registerReceiver(discoveryMonitor,
        new IntentFilter(dStarted));

    registerReceiver(discoveryMonitor,
        new IntentFilter(dFinished));

    startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE),  DISCOVERY_REQUEST);
    try {
        btserver = bluetooth.listenUsingRfcommWithServiceRecord(name, uuid);
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    Thread acceptThread = new Thread(new Runnable() 
    {
        public void run()
        {
            try {
               btserver.accept();
            } catch (IOException e){
         Log.d("BLUETOOTH", e.getMessage());
         }
         }
    });
    acceptThread.start();
}     
}
}