[英]Android Bluetooth as client i cant connect with my arduino
I have a problem, I can not establish a connection with my bluetooth shield, I've been with another application and if the device works, indicated by a light that is connected, but with this app, that does not happen. 我有一个问题,我无法与蓝牙屏蔽建立连接,我曾经与另一个应用程序连接过,并且如果该设备正常工作(由已连接的指示灯指示),但是与该应用程序连接,则不会发生。 the app crashes when this method is called mmSocket.connect (), if this is not bad socket acquiring tmp = mmDevice.createRfcommSocketToServiceRecord (MY_UUID);
如果调用tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID)的套接字还不错,则应用程序将在调用此方法(称为mmSocket.connect())时崩溃; please help i am new to this =)
请帮助我是新来的=)
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 = "";
}
} }
what is the UUID you are providing? 您提供的UUID是什么?
To get connected to any bluetooth profile running on a remote device, you need to establish socket connection with the UUID [of the service you want to connect.]. 要连接到在远程设备上运行的任何蓝牙配置文件,您需要使用[您要连接的服务的] UUID建立套接字连接。
For details list on UUID of different services, refer to assigned numbers.pdf in www.bluetooth.org 有关不同服务的UUID的详细列表,请参阅www.bluetooth.org中分配的number.pdf。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.