蓝牙插座连接

发布于 2024-10-01 19:55:19 字数 149 浏览 1 评论 0原文

我正在创建一个使用 Android 通过蓝牙发送和接收数据的应用程序。但我在创建套接字时遇到问题。他陷入了 mmSocket btserver.accept = (); 行中而且我无法与任何设备配对。

我还有一个疑问,我可以与android和symbian进行通信吗?

I'm creating an application that uses an android to send and receive data via bluetooth. but I'm having problems when creating the socket. He gets caught in that line mmSocket btserver.accept = (); And I can not pair with any device.

I have another doubt, I can make that communication with an android and a symbian?

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

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

发布评论

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

评论(1

情释 2024-10-08 19:55:19

代码如下。他找到了设备,但没有打开通信套接字。

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();
}     
}
}

The code is below. He finds devices but does not open the communication socket.

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