本文介绍了蓝牙套接字连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喂,我在这criando UMAaplicação阙美国庵段的android本身投递Ëreceber通过蓝牙dados。马斯我在这COM problemas呐霍拉德criarØ插座。 ELE菲卡travado萨linha
mmSocket = btserver.accept();
ËNAO consigo parear nenhum dispositivo。

Oi, estou criando uma aplicação que usa um android para se enviar e receber dados via bluetooth. mas estou com problemas na hora de criar o socket. Ele fica travado nessa linhammSocket = btserver.accept();E não consigo parear nenhum dispositivo.

Tenho outra duvida,欧盟posso FAZER ESSAcomunicaçãoCOM庵的Andr​​oidË微米的Symbian ??

Tenho outra duvida, eu posso fazer essa comunicação com um android e um symbian??

我创建一个使用一个机器人来发送和通过蓝牙接收数据的应用程序。但创建套接字时我有问题。他被捕后在该行mmSocket btserver.accept =();我不能与任何设备配对。

Hi, 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.

我还有一个疑问,我可以与Android和Symbian的沟通?

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

推荐答案

在code如下。他发现设备,但不打开通讯插座。

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

公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

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

}

}

这篇关于蓝牙套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:24