2014-11-17 1 views
1

주 동작시 잘 돌아가는 블루투스 예제 코드가 있습니다. 그런 다음 새로운 활동으로 옮기고 페이지를 실행하기위한 버튼을 만들었습니다. 나는 활동에 들어가 내 블루투스 기능을 활성화하기 위해 버튼을 클릭하지만이 선에 도달 할 때 충돌 할 수 있습니다새로운 활동으로 블루투스 코드를 옮겼습니다. 활성화시 충돌 발생

if (!myBluetoothAdapter.isEnabled())Set_Up_Connection.java 내부 그러나 라인 myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if(myBluetoothAdapter == null) 잘 작동.

MainActivity

package com.evenfurtherbeyond.bluetoothsendrecieve; 
 
//imports are created automatically when requested by functions 
 

 
import android.app.Activity; 
 
import android.bluetooth.BluetoothAdapter; 
 
import android.bluetooth.BluetoothDevice; 
 
import android.content.Intent; 
 
import android.os.Bundle; 
 
import android.view.View; 
 
import android.widget.Button; 
 

 
import java.util.Set; 
 

 
public class MainActivity extends Activity { 
 

 
    //define the settings button variable 
 
    private Button settingsBtn; 
 
    public BluetoothAdapter myBluetoothAdapter; 
 
    protected Set<BluetoothDevice> pairedDevices; 
 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     // take an instance of BluetoothAdapter - Bluetooth radio 
 
     myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
 
     //on the apps creation set the content view to the activity_my.xml layout. 
 
     setContentView(R.layout.activity_main); 
 

 
     //define settings button 
 
     settingsBtn = (Button) findViewById(R.id.settingsbt); 
 

 
     //set up an on click listener 
 
     settingsBtn.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View v) { 
 
       //define an intent to start a new activity 
 
       //for starting Activity because you will need to pass Application or component 
 
       // context as first parameter to Intent Constructor when you are creating 
 
       // intent for a specific component of application. 
 
       Intent settingsIntent = new Intent(v.getContext(), Set_Up_Connection.class); 
 
       //start the intent. 
 
       startActivity(settingsIntent); 
 
      } 
 
     }); 
 
    } 
 
}

Set_Up_Connection

package com.evenfurtherbeyond.bluetoothsendrecieve; 
 

 
import android.app.Activity; 
 
import android.bluetooth.BluetoothAdapter; 
 
import android.bluetooth.BluetoothDevice; 
 
import android.content.Intent; 
 
import android.os.Bundle; 
 
import android.view.View; 
 
import android.widget.ArrayAdapter; 
 
import android.widget.Button; 
 
import android.widget.ListView; 
 
import android.widget.TextView; 
 
import android.widget.Toast; 
 

 
import java.util.Set; 
 

 
public class Set_Up_Connection extends Activity { 
 
    private Button onBtn; 
 

 
    private TextView text; 
 
    private BluetoothAdapter myBluetoothAdapter; 
 
    protected Set<BluetoothDevice> pairedDevices; 
 
    //sets the paired device object as a bluetooth device object container. 
 
    protected ListView myListView; 
 
    private ArrayAdapter<String> BTArrayAdapter; 
 
//containers for variables, many of the variables relate to the buttons defined in the activity_my.xml. 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
    super.onCreate(savedInstanceState); 
 
    setContentView(R.layout.set_up_bt); 
 
    //on the apps creation set the content view to the activity_my.xml layout. 
 

 
     // take an instance of BluetoothAdapter - Bluetooth radio 
 
     myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
 
     //get the default adater string/id and place it in myBluetoothAdapter 
 
     if(myBluetoothAdapter == null) { 
 
      //if an adapter doesn't exist 
 
      onBtn.setEnabled(false); 
 
      text.setText("Status: not supported"); 
 
      Toast.makeText(getApplicationContext(), "Your device does not support Bluetooth", 
 
        Toast.LENGTH_LONG).show(); 
 
      //display a long notification that the device doesn't have bluetooth. 
 
     } else { 
 
      text = (TextView) findViewById(R.id.text); 
 
      //make the text variable equal the type 'TextView' of the 'R.id.text' id value. 
 
      //text =/= 'R.id.text but rather text = the address of 'R.id.text' 
 
      //whatever 'R.id.text' equals, 'text' now equals. 
 

 
      onBtn = (Button)findViewById(R.id.turnOn); 
 
      //onBtn = type 'Button' address 'R.id.turnOn' 
 
      onBtn.setOnClickListener(new View.OnClickListener() { 
 
       //set an onclick listener to do something. 
 

 
       @Override 
 
       public void onClick(View v) { 
 

 
        on(v); 
 
       } 
 
       //when the 'turnon' button is pressed run the method 'on' and pass it the current view 'v' 
 
      }); 
 

 

 
      myListView = (ListView)findViewById(R.id.listView1); 
 

 
      // create the arrayAdapter that contains the BTDevices, and set it to the ListView 
 
      BTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); 
 
      //create an array adapter object of type String using 'this' context and the 'simple list item 1' layout. 
 

 
      myListView.setAdapter(BTArrayAdapter); 
 
      //set the ListView to the Array Adapter creating a list of the 'BTArrayAdapter' array 
 
     } 
 
    } 
 

 
    public void on(View view) { 
 

 
     if (!myBluetoothAdapter.isEnabled()) { 
 

 
      //isEnabled checks if the bluetooth adapter is enabled. 
 
      Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
 
      //start an intent to show a system activity that allows the user to turn on Bluetooth. 
 
     } 
 
     else{ 
 
      Toast.makeText(getApplicationContext(),"Bluetooth is already on", 
 
        Toast.LENGTH_LONG).show(); 
 
      //else display a notification that BT is already on. 
 
     } 
 
    } 
 
}

AndroidManifest 

    <?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.evenfurtherbeyond.bluetoothsendrecieve" > 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Set_Up_Connection" 
      android:label="Set Up Bluetooth" 
      android:parentActivityName=".MainActivity"> 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.evenfurtherbeyond.MainActivity"/> 
     </activity> 

    </application> 

</manifest> 

로그 캣

01,235,
11-17 19:38:09.585 8472-8472/com.evenfurtherbeyond.bluetoothsendrecieve E/AndroidRuntime﹕ FATAL EXCEPTION: main 
java.lang.SecurityException: Need BLUETOOTH_ADMIN permission: Neither user 10084 nor current process has android.permission.BLUETOOTH_ADMIN. 
     at android.os.Parcel.readException(Parcel.java:1425) 
     at android.os.Parcel.readException(Parcel.java:1379) 
     at android.bluetooth.IBluetooth$Stub$Proxy.disable(IBluetooth.java:1048) 
     at android.bluetooth.BluetoothAdapter.disable(BluetoothAdapter.java:527) 
     at com.evenfurtherbeyond.bluetoothsendrecieve.Set_Up_Connection.off(Set_Up_Connection.java:211) 
     at com.evenfurtherbeyond.bluetoothsendrecieve.Set_Up_Connection$2.onClick(Set_Up_Connection.java:88) 
     at android.view.View.performClick(View.java:4209) 
     at android.view.View$PerformClick.run(View.java:17431) 
     at android.os.Handler.handleCallback(Handler.java:725) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:153) 
     at android.app.ActivityThread.main(ActivityThread.java:5297) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
     at dalvik.system.NativeStart.main(Native Method) 
+0

로그 랭크 및 관련 소스 코드를 게시하거나 그렇지 않으면 단지 추측 만합니다. –

+0

오류 메시지를 인쇄 할 수 있습니까? – Jithu

+0

예, 죄송합니다. 질문을 끝내기 전에 실수로 묻습니다. 관련 소스가 제공됩니다. 편집 : 글쎄, 지금은 정말 바보 같아. logcat을 읽고 나는 블루투스 권한이 필요하다고 본다. 이 질문을 삭제하거나 답변에서 편집해야합니까? – Automationispower

답변

1

누군가가 logcat을 요청한 후에 문제가 기록 된 것을 확인했습니다. 사용하여 블루투스와 Bluetooth_Admin 권한을 포함하는 매니페스트를 업데이트 한 후 : 응용 프로그램이 원활하게 실행 http://developer.android.com/guide/topics/security/permissions.html 튜토리얼에서

<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 

.

관련 문제