Android 蓝牙连接问题
我正在尝试打开里程碑和电脑之间的套接字蓝牙,但设备无法连接。我希望我能发送代码帮助。
public class Bluetooth_V2 extends Activity {
public TextView tela;
public ListView telaDevice;
public BluetoothAdapter bluetooth;
public String endBluetooth;
public String endDevice;
public String nomeDevice;
public ArrayAdapter<String> nomeBluetooth;
private static final String info = "DEBUG";
private static final int DISCOVERABLE_REQUEST = 0;
private static final int DISCOVERY_REQUEST = 0;
public BroadcastReceiver mReceiver = null;
public ArrayList<String> deviceDescoberto;
public BluetoothDevice deviceRemoto;
public BluetoothServerSocket btserver;
public BluetoothSocket serverSocket;
public UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
public OutputStream outStream;
public InputStream instream;
public BluetoothSocket socket;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tela = (TextView) findViewById(R.id.telaMsg);
telaDevice = (ListView) findViewById(R.id.telaDevice);
bluetooth = BluetoothAdapter.getDefaultAdapter();
nomeBluetooth = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
telaDevice.setAdapter(nomeBluetooth);
Log.d(info, "Recebeu adaptador");
if(bluetooth==null)
{
Log.d(info,"Bluetooth nao suportado");
tela.append("Bluetooth nao suportado");
}
if(!bluetooth.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,RESULT_OK);
}
tela.append("\nNome:"+bluetooth.getName());
tela.append("\nEndereço:"+bluetooth.getAddress());
bluetooth.startDiscovery();
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivity(discoverableIntent);
//discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
bluetooth.isDiscovering();
Log.d(info,"Estado de descoberta");
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
tela.append("\nModo de busca" + bluetooth.getScanMode());
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
//startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE),DISCOVERABLE_REQUEST);
tela.append("\nDispositivos encontrados:");
BroadcastReceiver discoveryResult = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
nomeDevice = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
deviceRemoto = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
tela.append("\n"+nomeDevice+"\n"+deviceRemoto);
// TODO Do something with the remote Bluetooth Device.
}
};
registerReceiver(discoveryResult,
new IntentFilter(BluetoothDevice.ACTION_FOUND));
if (!bluetooth.isDiscovering())
bluetooth.startDiscovery();
/* //Mostra dispositivos pareados
tela.append("\nDispositivos pareados mas não conectados");
Set<BluetoothDevice> devices = bluetooth.getBondedDevices();
for(BluetoothDevice device : devices){
tela.append("\n"+device.getName());
}*/
startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE),DISCOVERY_REQUEST);
final BluetoothDevice device = bluetooth.getRemoteDevice("90:4C:E5:FA:01:22");
final Set<BluetoothDevice> bondedDevices = bluetooth.getBondedDevices();
BroadcastReceiver Result = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
deviceRemoto = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(deviceRemoto.equals(device)&&bondedDevices.contains(deviceRemoto)){
tela.append("Dispositivos"+deviceRemoto.getName()+" ja está pareado");
}
}
};
registerReceiver(Result, new IntentFilter(BluetoothDevice.ACTION_FOUND));
try{
deviceRemoto = bluetooth.getRemoteDevice("90:4C:E5:FA:01:22");
BluetoothSocket clientSocket =
device.createRfcommSocketToServiceRecord(uuid);
clientSocket.connect();
Log.d(info,"Cliente Conectado");
//sendMessage("OI");
// TODO Transfer data using the Bluetooth Socket
} catch (IOException e) {
Log.d("BLUETOOTH CLIENTE", e.getMessage());
}
}
}
I'm trying to open a socket bluetooth between a milestone and a pc but the device can not connect. I hope I am sending the code help.
public class Bluetooth_V2 extends Activity {
public TextView tela;
public ListView telaDevice;
public BluetoothAdapter bluetooth;
public String endBluetooth;
public String endDevice;
public String nomeDevice;
public ArrayAdapter<String> nomeBluetooth;
private static final String info = "DEBUG";
private static final int DISCOVERABLE_REQUEST = 0;
private static final int DISCOVERY_REQUEST = 0;
public BroadcastReceiver mReceiver = null;
public ArrayList<String> deviceDescoberto;
public BluetoothDevice deviceRemoto;
public BluetoothServerSocket btserver;
public BluetoothSocket serverSocket;
public UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
public OutputStream outStream;
public InputStream instream;
public BluetoothSocket socket;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tela = (TextView) findViewById(R.id.telaMsg);
telaDevice = (ListView) findViewById(R.id.telaDevice);
bluetooth = BluetoothAdapter.getDefaultAdapter();
nomeBluetooth = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
telaDevice.setAdapter(nomeBluetooth);
Log.d(info, "Recebeu adaptador");
if(bluetooth==null)
{
Log.d(info,"Bluetooth nao suportado");
tela.append("Bluetooth nao suportado");
}
if(!bluetooth.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,RESULT_OK);
}
tela.append("\nNome:"+bluetooth.getName());
tela.append("\nEndereço:"+bluetooth.getAddress());
bluetooth.startDiscovery();
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivity(discoverableIntent);
//discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
bluetooth.isDiscovering();
Log.d(info,"Estado de descoberta");
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
tela.append("\nModo de busca" + bluetooth.getScanMode());
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
//startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE),DISCOVERABLE_REQUEST);
tela.append("\nDispositivos encontrados:");
BroadcastReceiver discoveryResult = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
nomeDevice = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
deviceRemoto = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
tela.append("\n"+nomeDevice+"\n"+deviceRemoto);
// TODO Do something with the remote Bluetooth Device.
}
};
registerReceiver(discoveryResult,
new IntentFilter(BluetoothDevice.ACTION_FOUND));
if (!bluetooth.isDiscovering())
bluetooth.startDiscovery();
/* //Mostra dispositivos pareados
tela.append("\nDispositivos pareados mas não conectados");
Set<BluetoothDevice> devices = bluetooth.getBondedDevices();
for(BluetoothDevice device : devices){
tela.append("\n"+device.getName());
}*/
startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE),DISCOVERY_REQUEST);
final BluetoothDevice device = bluetooth.getRemoteDevice("90:4C:E5:FA:01:22");
final Set<BluetoothDevice> bondedDevices = bluetooth.getBondedDevices();
BroadcastReceiver Result = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
deviceRemoto = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(deviceRemoto.equals(device)&&bondedDevices.contains(deviceRemoto)){
tela.append("Dispositivos"+deviceRemoto.getName()+" ja está pareado");
}
}
};
registerReceiver(Result, new IntentFilter(BluetoothDevice.ACTION_FOUND));
try{
deviceRemoto = bluetooth.getRemoteDevice("90:4C:E5:FA:01:22");
BluetoothSocket clientSocket =
device.createRfcommSocketToServiceRecord(uuid);
clientSocket.connect();
Log.d(info,"Cliente Conectado");
//sendMessage("OI");
// TODO Transfer data using the Bluetooth Socket
} catch (IOException e) {
Log.d("BLUETOOTH CLIENTE", e.getMessage());
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
createRFcommSocketToServiceRecord 方法在 Android 2.1/2.2 中不起作用。
有一种解决方法可以调用非公共方法来创建套接字:
这取自以下问题:
在 Android 中断开蓝牙套接字
问候,
迈克尔
The createRFcommSocketToServiceRecord method is not working in Android 2.1/2.2.
There is a workaround to call a non-public method to create the socket:
This is taken from the following question:
Disconnect a bluetooth socket in Android
Regards,
Michael
这有效
This works