0

我有一个问题,我无法与我的蓝牙屏蔽建立连接,我一直在使用另一个应用程序,如果设备工作,由连接的灯指示,但使用此应用程序,不会发生这种情况。调用此方法时应用程序崩溃 mmSocket.connect(),如果这不是坏套接字获取 tmp = mmDevice.createRfcommSocketToServiceRecord (MY_UUID); 请帮助我是新手=)

import java.io.IOException;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;


public class ConfigView extends Activity implements OnClickListener, OnLongClickListener , OnItemSelectedListener {


Button bnt1;
Spinner listDevicesFound;
public  BluetoothAdapter mBluetoothAdapter;
private  BluetoothSocket mmSocket;
private  BluetoothDevice mmDevice;
private final UUID MY_UUID = UUID.fromString("6170d0f0-5bc3-11e2-bcfd-0800200c9a66");
ArrayAdapter<String> btArrayAdapter;
String deviceToConnect;

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

    bnt1 =(Button) findViewById(R.id.button1);
    bnt1.setOnClickListener(this);
    bnt1.setOnLongClickListener(this);
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    listDevicesFound = (Spinner) findViewById(R.id.spinner1);
    listDevicesFound.setOnItemSelectedListener(this);
    btArrayAdapter = new ArrayAdapter<String>(ConfigView.this, android.R.layout.simple_list_item_1);
    listDevicesFound.setAdapter(btArrayAdapter);
    registerReceiver(ActionFoundReceiver,new IntentFilter(BluetoothDevice.ACTION_FOUND));

    //ScanDevices 
    btArrayAdapter.clear();
    mBluetoothAdapter.startDiscovery();

}

@Override
protected void onDestroy() {
 // TODO Auto-generated method stub
 super.onDestroy();
 unregisterReceiver(ActionFoundReceiver);
}

//Buttons Listeners
@Override
public void onClick(View v) {
    if (v.getId() == R.id.button1)
    { 
      String address = deviceToConnect.substring(deviceToConnect.length() - 17);
      Toast.makeText(getApplicationContext(), address, Toast.LENGTH_SHORT).show();
      mmDevice = mBluetoothAdapter.getRemoteDevice(address);

      BluetoothSocket tmp = null;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) { }
        mmSocket = tmp;
    }

    mBluetoothAdapter.cancelDiscovery();

    try {
        // Connect the device through the socket. This will block
        // until it succeeds or throws an exception
        mmSocket.connect();
    } catch (IOException connectException) {
        // Unable to connect; close the socket and get out
        try {
            mmSocket.close();
        } catch (IOException closeException) { }
        return;
    }

}
@Override
public boolean onLongClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId() == R.id.button1)
    { 
         try {
             mmSocket.close();
         } catch (IOException e) { }
    }
    return true;
}

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver(){

      @Override
      public void onReceive(Context context, Intent intent) {
       // TODO Auto-generated method stub
       String action = intent.getAction();
       if(BluetoothDevice.ACTION_FOUND.equals(action)) {
                 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                 btArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                 btArrayAdapter.notifyDataSetChanged();
             }
      }};


public void onItemSelected(AdapterView<?> arg0, View v, int position,
        long id) {
    // TODO Auto-generated method stub
    deviceToConnect = btArrayAdapter.getItem(position);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
   deviceToConnect = ""; 
}

}

4

1 回答 1

0

您提供的 UUID 是什么?

要连接到远程设备上运行的任何蓝牙配置文件,您需要使用 UUID [您要连接的服务的] 建立套接字连接。

有关不同服务的 UUID 的详细列表,请参阅 www.bluetooth.org 中的分配编号.pdf

于 2013-01-15T12:52:12.877 回答