2013-07-10 105 views
0

我對android編程有點新,但現在我想嘗試編程一個應用程序,通過藍牙連接我的手機(三星Galaxy Note 2)與電路板。我只想打開/關閉一些LED。我在電路板上使用了一個bc417調制解調器。藍牙連接錯誤

我已經搜索了藍牙的例子,但他們似乎都如此困難,我只是想要我需要的命令。但是目前我的應用程序在ListView上選擇一個項目時一直崩潰。

有人可以幫我嗎?

package com.test.bluetoothtest; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Set; 
import java.util.UUID; 


import android.os.Bundle; 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemClickListener; 
import android.bluetooth.BluetoothSocket; 

public class MainActivity extends Activity { 

private static final int REQUEST_ENABLE_BT = 1; 
private static final UUID myuuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if(mBluetoothAdapter == null) 
    { 
     Toast.makeText(MainActivity.this, "Bluetooth couldn't be started.", Toast.LENGTH_SHORT).show(); 
    } 
    if(!mBluetoothAdapter.isEnabled()) 
    { 
     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     Toast.makeText(MainActivity.this, "Bluetooth started succesfully", Toast.LENGTH_SHORT).show(); 
    } 
    List<String> devicelist = new ArrayList<String>(); 

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
    if(pairedDevices.size() > 0) 
    { 
     for(BluetoothDevice device : pairedDevices) 
     { 
      devicelist.add(device.getName()); 

     } 
    } 
    ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, devicelist); 
    final ListView lv = (ListView)findViewById(R.id.listView1); 
    lv.setAdapter(adapter); 
    lv.setOnItemClickListener(new OnItemClickListener() 
    { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 
      try 
      { 
       Toast.makeText(MainActivity.this, lv.getAdapter().getItem(arg2).toString(), Toast.LENGTH_SHORT).show(); 
       BluetoothDevice finaldevice = (BluetoothDevice) lv.getAdapter().getItem(arg2);     
       BluetoothSocket clientSocket = finaldevice.createRfcommSocketToServiceRecord(myuuid); 
       clientSocket.connect(); 
       Toast.makeText(MainActivity.this, "Connectin successful!", Toast.LENGTH_SHORT).show(); 
      } 
      catch(IOException e) 
      { 
       Toast.makeText(MainActivity.this, "Error!", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

}

目錄下載:

07-10 20:38:55.115: D/AbsListView(16942): Get MotionRecognitionManager 
07-10 20:38:55.335: D/libEGL(16942): loaded /system/lib/egl/libEGL_mali.so 
07-10 20:38:55.345: D/libEGL(16942): loaded /system/lib/egl/libGLESv1_CM_mali.so 
07-10 20:38:55.345: D/libEGL(16942): loaded /system/lib/egl/libGLESv2_mali.so 
07-10 20:38:55.350: D/(16942): Device driver API match 
07-10 20:38:55.350: D/(16942): Device driver API version: 10 
07-10 20:38:55.350: D/(16942): User space API version: 10 
07-10 20:38:55.350: D/(16942): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
07-10 20:38:55.400: D/OpenGLRenderer(16942): Enabling debug mode 0 
07-10 20:39:05.145: D/GestureDetector(17303): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0 
07-10 20:39:05.240: D/AndroidRuntime(17303): Shutting down VM 
07-10 20:39:05.240: W/dalvikvm(17303): threadid=1: thread exiting with uncaught exception (group=0x40db82a0) 
07-10 20:39:05.245: E/AndroidRuntime(17303): FATAL EXCEPTION: main 
07-10 20:39:05.245: E/AndroidRuntime(17303): java.lang.ClassCastException: java.lang.String cannot be cast to android.bluetooth.BluetoothDevice 
07-10 20:39:05.245: E/AndroidRuntime(17303): at com.test.bluetoothtest.MainActivity$1.onItemClick(MainActivity.java:74) 
07-10 20:39:05.245: E/AndroidRuntime(17303): at android.widget.AdapterView.performItemClick(AdapterView.java:298) 
07-10 20:39:05.245: E/AndroidRuntime(17303): at android.widget.AbsListView.performItemClick(AbsListView.java:1283) 
07-10 20:39:05.245: E/AndroidRuntime(17303): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3074) 
07-10 20:39:05.245: E/AndroidRuntime(17303): at android.widget.AbsListView$1.run(AbsListView.java:4147) 
07-10 20:39:05.245: E/AndroidRuntime(17303): at android.os.Handler.handleCallback(Handler.java:615) 
07-10 20:39:05.245: E/AndroidRuntime(17303): at android.os.Handler.dispatchMessage(Handler.java:92) 
07-10 20:39:05.245: E/AndroidRuntime(17303): at android.os.Looper.loop(Looper.java:137) 
07-10 20:39:05.245: E/AndroidRuntime(17303): at android.app.ActivityThread.main(ActivityThread.java:4898) 
07-10 20:39:05.245: E/AndroidRuntime(17303): at java.lang.reflect.Method.invokeNative(Native Method) 
07-10 20:39:05.245: E/AndroidRuntime(17303): at java.lang.reflect.Method.invoke(Method.java:511) 
07-10 20:39:05.245: E/AndroidRuntime(17303): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008) 
07-10 20:39:05.245: E/AndroidRuntime(17303): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775) 
07-10 20:39:05.245: E/AndroidRuntime(17303): at dalvik.system.NativeStart.main(Native Method) 
07-10 20:41:33.045: D/AbsListView(18494): Get MotionRecognitionManager 
07-10 20:41:33.155: D/libEGL(18494): loaded /system/lib/egl/libEGL_mali.so 
07-10 20:41:33.160: D/libEGL(18494): loaded /system/lib/egl/libGLESv1_CM_mali.so 
07-10 20:41:33.165: D/libEGL(18494): loaded /system/lib/egl/libGLESv2_mali.so 
07-10 20:41:33.170: D/(18494): Device driver API match 
07-10 20:41:33.170: D/(18494): Device driver API version: 10 
07-10 20:41:33.170: D/(18494): User space API version: 10 
07-10 20:41:33.170: D/(18494): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
07-10 20:41:33.225: D/OpenGLRenderer(18494): Enabling debug mode 0 
07-10 20:41:34.975: D/GestureDetector(18494): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0 
07-10 20:41:35.110: D/AndroidRuntime(18494): Shutting down VM 
07-10 20:41:35.110: W/dalvikvm(18494): threadid=1: thread exiting with uncaught exception (group=0x40db82a0) 
07-10 20:41:35.115: E/AndroidRuntime(18494): FATAL EXCEPTION: main 
07-10 20:41:35.115: E/AndroidRuntime(18494): java.lang.ClassCastException: java.lang.String cannot be cast to android.bluetooth.BluetoothDevice 
07-10 20:41:35.115: E/AndroidRuntime(18494): at com.test.bluetoothtest.MainActivity$1.onItemClick(MainActivity.java:75) 
07-10 20:41:35.115: E/AndroidRuntime(18494): at android.widget.AdapterView.performItemClick(AdapterView.java:298) 
07-10 20:41:35.115: E/AndroidRuntime(18494): at android.widget.AbsListView.performItemClick(AbsListView.java:1283) 
07-10 20:41:35.115: E/AndroidRuntime(18494): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3074) 
07-10 20:41:35.115: E/AndroidRuntime(18494): at android.widget.AbsListView$1.run(AbsListView.java:4147) 
07-10 20:41:35.115: E/AndroidRuntime(18494): at android.os.Handler.handleCallback(Handler.java:615) 
07-10 20:41:35.115: E/AndroidRuntime(18494): at android.os.Handler.dispatchMessage(Handler.java:92) 
07-10 20:41:35.115: E/AndroidRuntime(18494): at android.os.Looper.loop(Looper.java:137) 
07-10 20:41:35.115: E/AndroidRuntime(18494): at android.app.ActivityThread.main(ActivityThread.java:4898) 
07-10 20:41:35.115: E/AndroidRuntime(18494): at java.lang.reflect.Method.invokeNative(Native Method) 
07-10 20:41:35.115: E/AndroidRuntime(18494): at java.lang.reflect.Method.invoke(Method.java:511) 
07-10 20:41:35.115: E/AndroidRuntime(18494): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008) 
07-10 20:41:35.115: E/AndroidRuntime(18494): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775) 
07-10 20:41:35.115: E/AndroidRuntime(18494): at dalvik.system.NativeStart.main(Native Method) 
07-10 20:41:47.780: I/Process(18494): Sending signal. PID: 18494 SIG: 9 
+0

歡迎來到Stackoverflow。我知道你是新人,但要記住,你的一些研究可以幫助你解決問題。你得到的錯誤是什麼?請先顯示你的努力,以便其他人可以幫助你。此外,請閱讀[FAQ](http://stackoverflow.com/help)和[如何提問](http://stackoverflow.com/questions/how-to-ask) – kgdesouz

回答

1

你的問題是以下行

BluetoothDevice finaldevice = (BluetoothDevice) lv.getAdapter().getItem(arg2); 

的getItem返回字符串不是BluetoothDevice類。你的演員失敗。

請執行以下操作。

您查詢配對的設備並在列表視圖中顯示它們。從配對的設備,您需要它的Mac地址爲了創建連接。在遍歷配對設備時,可以提取每個人的地址。稍後在您的點擊進行以下操作以獲取BluetoothDevice。

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
// If there are paired devices 
if (pairedDevices.size() > 0) { 
    // Loop through paired devices 
    for (BluetoothDevice device : pairedDevices) { 
     // Add the name and address to an array adapter to show in a ListView 
     mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
    } 
} 

稍後從項目的文本中點擊一個項目並相應地使用它時提取此地址。

BluetoothDevice device = btAdapter.getRemoteDevice(address); 

然後用下面的

btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); 

得到的BluetoothSocket然後你就可以使用這個插座進行連接。

btSocket.connect(); 
+0

好吧,這是有道理的。但我怎麼得到BluetoothDevice那麼? – Sensoume

+0

非常感謝! – Sensoume

0

它會很高興知道你的應用程序崩潰。 LogCat也可以是有幫助的。 因爲我有自己的UUID我與

Method m = finaldevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}); 
clientSocket = (Bluetoothsocket) m.invoke(finaldevice, Integer.valueOf(1)); 
clientSocket.connect(); 

它爲我工作的一些問題。

+0

eclipse告訴我把一些catch子句我的應用程序崩潰,如果我點擊一個項目在列表視圖,意味着在那裏公共無效onItemClick函數 – Sensoume