2017-08-27 181 views
-1

我試圖構建一個應用程序,讓您通過藍牙將數據發送到 arduino。但是我在創建套接字時被卡住了。該應用程序在啓動時保持 崩潰,但我不知道爲什麼。藍牙Android應用程序在啓動時不斷崩潰

package de.lutherschule.bled.bluetoothled; 

import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.content.Intent; 
import android.icu.util.Output; 
import android.support.annotation.WorkerThread; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.Toast; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.Set; 
import java.util.UUID; 

public class MainActivity extends AppCompatActivity { 

BluetoothAdapter bAdapter; 
BluetoothDevice device; 
Boolean found; 
BluetoothSocket btSocket; 

OutputStream outputStream; 
InputStream inputStream; 

public static final String DEVICE_ADRESS = ""; 
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
private static final String TAG = "MainActvity"; 


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

    bAdapter = BluetoothAdapter.getDefaultAdapter(); 

    found = false; 
    btSocket = null; 

    Log.v(TAG, "Funst"); 

    if (bAdapter == null) { 
     Toast.makeText(getApplicationContext(),"Funst net",Toast.LENGTH_SHORT).show(); 
    } 

    if(!bAdapter.isEnabled()) { 
     Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableAdapter, 0); 
    } 

    Set<BluetoothDevice> bondedDevices = bAdapter.getBondedDevices(); 

    if(bondedDevices.isEmpty()) { 

     Toast.makeText(getApplicationContext(),"Muss gepaired werden",Toast.LENGTH_SHORT).show(); 

    } else { 

     for (BluetoothDevice iter : bondedDevices) { 

      if(iter.getAddress().equals(DEVICE_ADRESS)) { // oder iter.getName() wenn Name bekannt muessen dann aendern 

       device=iter; 
       found=true; 
       break; 

      } 
     } 
    } 

當我刪除此報價下的部分。該應用程序開始就好。

 try{ 
      btSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID); 

     }catch(IOException ioe){ 
      Log.v(TAG, "socket nit erstellt"); 
     } 


    } 

    @Override 
    public void onResume() { 
     super.onResume(); 


     Intent intent = getIntent(); 

     device = bAdapter.getRemoteDevice(DEVICE_ADRESS); 

     try { 
      btSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID); 
     } catch (IOException e) { 
      Toast.makeText(getBaseContext(), "Socket nit erstellt", Toast.LENGTH_LONG).show(); 
     } 

     try { 
     btSocket.connect(); 
     } catch (IOException e) { 
      try { 
       btSocket.close(); 
      } catch (IOException e2) { 

      } 
     } 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     try { 
      btSocket.close(); 
     } catch (IOException e2) { 
     } 
    } 

} 

我會很感激的任何幫助。順便提一下我的格式......我是創建帖子的新手。

的Android顯示器:

08-27 17:56:51.857 3071-3071/? E/libprocessgroup: failed to make and chown /acct/uid_10058: Read-only file system 
08-27 17:56:51.857 3071-3071/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT? 
08-27 17:56:51.857 3071-3071/? I/art: Not late-enabling -Xcheck:jni (already on) 
08-27 17:56:52.086 3071-3071/de.lutherschule.bled.bluetoothled I/InstantRun: Starting Instant Run Server for de.lutherschule.bled.bluetoothled 
08-27 17:56:52.288 3071-3071/de.lutherschule.bled.bluetoothled W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 

                     [ 08-27 17:56:52.386 1162: 2146 D/   ] 
                     HostConnection::get() New Host Connection established 0xb68aea60, tid 2146 
08-27 17:56:52.429 3071-3079/de.lutherschule.bled.bluetoothled W/art: Suspending all threads took: 17.085ms 
08-27 17:56:52.442 3071-3071/de.lutherschule.bled.bluetoothled E/BluetoothAdapter: Bluetooth binder is null 
08-27 17:56:52.442 3071-3071/de.lutherschule.bled.bluetoothled V/MainActvity: Funst 
08-27 17:56:52.452 3071-3071/de.lutherschule.bled.bluetoothled D/AndroidRuntime: Shutting down VM 

---------崩潰的開始

08-27 17:56:52.453 3071-3071/de.lutherschule.bled.bluetoothled E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: de.lutherschule.bled.bluetoothled, PID: 3071 
                       java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lutherschule.bled.bluetoothled/de.lutherschule.bled.bluetoothled.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothAdapter.isEnabled()' on a null object reference 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                        at android.os.Looper.loop(Looper.java:135) 
                        at android.app.ActivityThread.main(ActivityThread.java:5254) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at java.lang.reflect.Method.invoke(Method.java:372) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothAdapter.isEnabled()' on a null object reference 
                        at de.lutherschule.bled.bluetoothled.MainActivity.onCreate(MainActivity.java:59) 
                        at android.app.Activity.performCreate(Activity.java:5990) 
                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  
                        at android.app.ActivityThread.access$800(ActivityThread.java:151)  
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  
                        at android.os.Handler.dispatchMessage(Handler.java:102)  
                        at android.os.Looper.loop(Looper.java:135)  
                        at android.app.ActivityThread.main(ActivityThread.java:5254)  
                        at java.lang.reflect.Method.invoke(Native Method)  
                        at java.lang.reflect.Method.invoke(Method.java:372)  
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  
+0

你可以發佈異常,你在Android顯示器? –

+0

我已經發布了它們! –

+0

你的設備中是否有藍牙,它是否啓用? 請檢查這個 https://stackoverflow.com/questions/24087445/bluetoothadapter-getdefaultadapter-not-returning-null –

回答

0

嘗試使用

BluetoothDevice device = mAdapter.getRemoteDevice(address); 
btsocket = device.createRfcommSocketToServiceRecord(MY_UUID); 
socket.connect(); 

這是我的代碼。

   try { 
         socket = connect_device.createRfcommSocketToServiceRecord(MY_UUID); 
         socket.connect(); 
         h.sendEmptyMessage(0); 

        } catch (IOException e) { 
         // TODO Auto-generated catch block 
        h.sendEmptyMessage(1); 
         e.printStackTrace(); 
         try { 
          socket.close(); 
         } catch (IOException e2) { 

         } 

        }