【问题标题】:Android getting serial data via bluetooth using UUIDAndroid使用UUID通过蓝牙获取串行数据
【发布时间】:2014-04-10 12:03:12
【问题描述】:

我是安卓蓝牙域的新手。我可以使用 UUID 列出配对的蓝牙设备。我的目标是通过蓝牙获取串口数据并发送。

我关注了这个reference link

我的代码:

import java.util.ArrayList;
import java.util.Iterator;
import com.example.discoverdevicesandservices.R;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;

public class DiscoverDevicesAndServices extends Activity {
TextView out;
private static final int REQUEST_ENABLE_BT = 1;
private BluetoothAdapter btAdapter; 
private ArrayList<BluetoothDevice> btDeviceList = new ArrayList<BluetoothDevice>();

*//** Called when the activity is first created. *//*
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

out = (TextView) findViewById(R.id.out);
//out.setMovementMethod(new ScrollingMovementMethod());

//Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_UUID);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(ActionFoundReceiver, filter); 
// Getting the Bluetooth adapter
btAdapter = BluetoothAdapter.getDefaultAdapter();
out.append("\nAdapter: " + btAdapter);

CheckBTState();
}

This routine is called when an activity completes.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BT) {
  CheckBTState();
}
}

 Override
 protected void onDestroy() {
 super.onDestroy();
 if (btAdapter != null) {
  btAdapter.cancelDiscovery();
 }
 unregisterReceiver(ActionFoundReceiver);
 }

  private void CheckBTState() {
 // Check for Bluetooth support and then check to make sure it is turned on
 // If it isn't request to turn it on
  // List paired devices
// Emulator doesn't support Bluetooth and will return null
 if(btAdapter==null) { 
  out.append("\nBluetooth NOT supported. Aborting.");
  return;
 } else {
  if (btAdapter.isEnabled()) {
    out.append("\nBluetooth is enabled...");

    // Starting the device discovery
    btAdapter.startDiscovery();
  } else {
    Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  }
}
}

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver(){

@Override
public void onReceive(Context context, Intent intent) {
 String action = intent.getAction();
 if(BluetoothDevice.ACTION_FOUND.equals(action)) {
   BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
   out.append("\n  Device: " + device.getName() + ", " + device);
   btDeviceList.add(device);
 } else {
   if(BluetoothDevice.ACTION_UUID.equals(action)) {
     BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
     Parcelable[] uuidExtra =      intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
     for (int i=0; i<uuidExtra.length; i++) {
       out.append("\n  Device: " + device.getName() + ", " + device + ", Service: " + uuidExtra[i].toString());
     }
   } else {
     if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
       out.append("\nDiscovery Started...");
     } else {
       if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
         out.append("\nDiscovery Finished");
         Iterator<BluetoothDevice> itr = btDeviceList.iterator();
         while (itr.hasNext()) {
           // Get Services for paired devices
           BluetoothDevice device = itr.next();
           out.append("\nGetting Services for " + device.getName() + ", " + device);
           if(!device.fetchUuidsWithSdp()) {
             out.append("\nSDP Failed for " + device.getName());
           }

         }
       }
     }
   }
  }
 }
};

}

请帮忙!!谢谢。

【问题讨论】:

  • 您知道 SO 规则要求发帖者在寻求答案之前至少先尝试过某事。 ;)
  • 这里有很多关于这个主题的问题,这些问题比您的尝试更进一步。通过阅读它们和其他网络资源,您可能会学到很多东西。
  • 如果您有更具体的问题也会有所帮助

标签: android bluetooth serial-port uuid android-bluetooth


【解决方案1】:

您已经使用以下代码部分获得了蓝牙设备的详细信息,使用此对象与串行端口配置文件中的蓝牙设备进行通信

"

BluetoothDevice 设备 = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

"

对象“设备”是您要连接的蓝牙设备。 一般来说,串口配置文件的 UUID 是 00001101-0000-1000-8000-00805F9B34FB

> BluetoothSocket Bsock = null;
> private static final UUID MY_UUID_SPP=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); >InputStream tmpIn = null; 
>OutputStream tmpOut = null;
> 
> 
> try{ 
>Bsock = device.createRfcommSocketToServiceRecord(MY_UUID_SPP); 
> Bsock.connect();
> tmpIn = socket.getInputStream(); 
>tmpOut = socket.getOutputStream(); 
>//use input stream and output stream to read and write to dataserially
> 
> }catch(IOException e){e.printStackTrace()}

【讨论】:

    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    相关资源
    最近更新 更多