2013-01-12 3 views
0

나는 내 블루투스 방패와 연결을 설정할 수 없다, 나는 다른 응용 프로그램과 함께 있었고, 장치가 작동하면 연결되어있는 빛에 의해 표시, 하지만이 응용 프로그램과 함께, 그것은 발생하지 않습니다. 이 메소드가 불량 소켓이 아닌 경우,이 메소드가 mmSocket.connect()라고 불리면 앱이 중단됩니다. tmp = mmDevice.createRfcommSocketToServiceRecord (MY_UUID); 안드로이드 블루투스 클라이언트로 나는 내 arduino와 연결

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

}

답변

0

UUID를 당신이 무엇을 제공하고있다) = 나는이 새로운 오전 도와주세요?

원격 장치에서 실행중인 모든 Bluetooth 프로필에 연결하려면 [연결하려는 서비스의 UUID]와 소켓 연결을 설정해야합니다.

다른 서비스의 UUID에 대한 자세한 내용은 www.bluetooth.org의 assigned numbers.pdf를 참조하십시오.

+0

어떻게받을 수 있습니까 ?? – Christ777porto

+0

https://bluetooth.org/Technical/AssignedNumbers/home.htm –

+0

해결되었습니다. 대단히 감사합니다! – Christ777porto

관련 문제