2013-08-23 2 views
2

블루투스 응용 프로그램을 개발 중입니다. 내 응용 프로그램에서 다음 코드가 있습니다. 블루투스가 꺼져있을 때 블루투스에서 내 else 문을 바꿔서 장치 목록을 발견하면 (잘 작동 함) 그렇지 않으면 명령문이 실행되어 장치 목록을 가져옵니다.블루투스 검색이 작동하지 않습니다.

My problem is "if condition is runnning but it doesn't discover the devices" 

. 누구나 해결책을 제안 할 수 있습니다.

if (btAdapter.isEnabled()) { 

     registerReceiver(ActionFoundReceiver, new IntentFilter(
       BluetoothDevice.ACTION_FOUND)); 
     btAdapter.startDiscovery(); 
    } else { 
     Intent i1 = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivity(i1); 

     new java.util.Timer().schedule(new java.util.TimerTask() { 
      @Override 
      public void run() { 
       registerReceiver(ActionFoundReceiver, new IntentFilter(
         BluetoothDevice.ACTION_FOUND)); 
       btAdapter.startDiscovery(); 
      } 
     }, 5000); 

    } 

는 // 수신 장치 코드를 탐지 여기 가정

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     String action = intent.getAction(); 

     if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      BluetoothDevice device = intent 
        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      items += device.getName() + ","; 

     } 
     Toast.makeText(getApplicationContext(), items, Toast.LENGTH_LONG) 
       .show(); 
    } 

}; 

답변

1

하실 것을 적극 추천.

Bluetooth should never be enabled without direct user consent. If you 

want to turn on Bluetooth in order to create a wireless connection, you should use the ACTION_REQUEST_ENABLE Intent, which will raise a dialog that requests user permission to turn on Bluetooth. The enable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app. 

을하지만 당신이 정말로 그것을 수행하려는 경우, 방법이 :

http://developer.android.com/guide/topics/wireless/bluetooth.html#FindingDevices

사용자에게 묻지 않고 블루투스를 활성화하는 방법에 대한

http://developer.android.com/guide/topics/wireless/bluetooth.html#DiscoveringDevices

, 여기에 문서의 말씀입니다 :

BluetoothAdapter.enable()

당신의 순서를 사용자

을 요청하지 않고이 문제를 호출 할 수 있도록() 당신은 안드로이드 매니페스트 파일

"android.permission.BLUETOOTH_ADMIN"

그것을 시도에 권한을 추가해야 작동합니다 , 답장을 보내

getApplicationContext().registerReceiver(receiver, 
        new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED)); 
getApplicationContext().registerReceiver(receiver, 
        new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED)); 
+0

덕분에 ... 이미 위의 링크를 참조 ... – Aravin

+0

아무것도 일어나지 ... 조건없는 장치 검색 목록 경우 ... 같은 condition..in 경우 내 문제에 . – Aravin

관련 문제