本文介绍了与蓝牙Android的连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想打开一个里程碑,一个电脑,但该设备无法连接之间的接口蓝牙。我希望我送了code的帮助。
公共类Bluetooth_V2延伸活动{
公共TextView的屏幕截图;
公众的ListView telaDevice;
公共BluetoothAdapter蓝牙;
公共字符串endBluetooth;
公共字符串endDevice;
公共字符串nomeDevice;
公共ArrayAdapter<字符串> nomeBluetooth;
私有静态最后弦乐信息=DEBUG;
私有静态最终诠释DISCOVERABLE_REQUEST = 0;
私有静态最终诠释DISCOVERY_REQUEST = 0;
公众的BroadcastReceiver mReceiver = NULL;
公众的ArrayList<字符串> deviceDescoberto;
公共BluetoothDevice类deviceRemoto;
公共BluetoothServerSocket btserver;
公众的BluetoothSocket ServerSocket的;
公共UUID UUID = UUID.fromString(00001101-0000-1000-8000-00805F9B34FB);
公众的OutputStream outStream;
公众的InputStream河道内;
公众的BluetoothSocket插座;
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
影视处=(TextView中)findViewById(R.id.telaMsg);
telaDevice =(ListView控件)findViewById(R.id.telaDevice);
蓝牙= BluetoothAdapter.getDefaultAdapter();
nomeBluetooth =新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1);
telaDevice.setAdapter(nomeBluetooth);
Log.d(信息,Recebeu adaptador);
如果(蓝牙== NULL)
{
Log.d(信息,蓝牙NAO suportado);
tela.append(蓝牙NAO suportado);
}
如果(!bluetooth.isEnabled())
{
意图enableBtIntent =新的意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,RESULT_OK);
}
tela.append(\ nNome:+ bluetooth.getName());
tela.append(\nEndereço:+ bluetooth.getAddress());
bluetooth.startDiscovery();
意图discoverableIntent =新的意图(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivity(discoverableIntent);
//discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
bluetooth.isDiscovering();
Log.d(信息,斯卡德descoberta);
IntentFilter的过滤器=新的IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver,过滤器);
tela.append(\ nModo德布斯卡+ bluetooth.getScanMode());
过滤器=新的IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver,过滤器);
// startActivityForResult(新意图(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE),DISCOVERABLE_REQUEST);
tela.append(\ nDispositivos encontrados:);
BroadcastReceiver的discoveryResult =新的BroadcastReceiver(){
公共无效的onReceive(上下文的背景下,意图意图){
nomeDevice = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
deviceRemoto = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
tela.append(\ N+ nomeDevice +\ N+ deviceRemoto);
// TODO做一些与远程蓝牙设备。
}
};
registerReceiver(discoveryResult,
新的IntentFilter(BluetoothDevice.ACTION_FOUND));
如果(!bluetooth.isDiscovering())
bluetooth.startDiscovery();
/ * // MOSTRA dispositivos pareados
tela.append(\ nDispositivos pareados MAS NAO conectados);
设置< BluetoothDevice类>器件= bluetooth.getBondedDevices();
对于(BluetoothDevice类设备:设备){
tela.append(\ N+ device.getName());
} * /
startActivityForResult(新意图(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE),DISCOVERY_REQUEST);
最后BluetoothDevice类设备= bluetooth.getRemoteDevice(90:4C:E5:FA:01:22);
决胜盘< BluetoothDevice类> bondedDevices = bluetooth.getBondedDevices();
BroadcastReceiver的结果=新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
deviceRemoto = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
如果(deviceRemoto.equals(设备)及&安培; bondedDevices.contains(deviceRemoto)){
tela.append(Dispositivos+ deviceRemoto.getName()+JA ESTA pareado);
}
}
};
registerReceiver(因此,新的IntentFilter(BluetoothDevice.ACTION_FOUND));
尝试{
deviceRemoto = bluetooth.getRemoteDevice(90:4C:E5:FA:01:22);
的BluetoothSocket ClientSocket的=
device.createRfcommSocketToServiceRecord(UUID);
clientSocket.connect();
Log.d(信息,Cliente Conectado);
//的sendMessage(成骨不全症);
使用蓝牙套接字// TODO传输数据
}赶上(IOException异常E){
Log.d(BLUETOOTH CLIENTE,e.getMessage());
}
}
}
解决方案
该createRFcommSocketToServiceRecord方法不是工作在了Android 2.1 / 2.2。有一种变通方法来调用一个非公开的方法来创建套接字:
BluetoothDevice类HXM = BluetoothAdapter.getDefaultAdapter()getRemoteDevice(device.getAddress())。
法米;
M = hxm.getClass()实现getMethod(createRfcommSocket,新的等级[] {} int.class)。
插座=(的BluetoothSocket)m.invoke(HXM,Integer.valueOf(1));
这是取自以下问题:Disconnect蓝牙插座的Android
问候,迈克尔
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());
}
}
}
解决方案
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:
BluetoothDevice hxm = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());
Method m;
m = hxm.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
socket = (BluetoothSocket)m.invoke(hxm, Integer.valueOf(1));
This is taken from the following question:Disconnect a bluetooth socket in Android
Regards,Michael
这篇关于与蓝牙Android的连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!