簡體   English   中英

Android藍牙作為客戶端,我無法與arduino連接

[英]Android Bluetooth as client i cant connect with my arduino

我有一個問題,我無法與藍牙屏蔽建立連接,我曾經與另一個應用程序連接過,並且如果該設備正常工作(由已連接的指示燈指示),但是與該應用程序連接,則不會發生。 如果調用tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID)的套接字還不錯,則應用程序將在調用此方法(稱為mmSocket.connect())時崩潰; 請幫助我是新來的=)

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 = ""; 
}

}

您提供的UUID是什么?

要連接到在遠程設備上運行的任何藍牙配置文件,您需要使用[您要連接的服務的] UUID建立套接字連接。

有關不同服務的UUID的詳細列表,請參閱www.bluetooth.org中分配的number.pdf。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM