2012-06-11 5 views
1

사용 가능한 모든 Bluetooth 장치를 찾은 다음 사용자가 목록의 요소를 클릭하면 장치를 선택한 주소와 페어링하고 검색을 취소하고 싶습니다. 그러나 Bluetooth 어댑터의 cancelDiscovery() 호출은 항상 false를 반환합니다. 안드로이드 워드 프로세서에서는 cancelDiscovery()가 true를 반환하도록 어댑터가 STATE_ON에 있어야한다고 말합니다. 그러나 btAdapter.getState()를 호출하면 값 12 (STATE_ON)가 반환됩니다. 내 코드는 아래에 있으며, 그 밖의 무엇이 잘못 될 수 있는지 아는 사람이 있습니까?cancelDiscovery는 false를 계속 반환합니다.

현재 상태가 STATE_ON 인 경우 cancelDiscovery()가 false를 반환하는 방식을 이해하지 못합니다. 문서에서는 상태가 STATE_ON이면 true를 반환하고 다른 값이면 false를 반환한다고 설명합니다.

private static final UUID MY_UUID = UUID.randomUUID(); 

private static final int REQUEST_ENABLE_BT = 1; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    btnScanDevice = (Button) findViewById(R.id.scandevice); 

    stateBluetooth = (TextView) findViewById(R.id.bluetoothstate); 
    startBluetooth(); 

    listDevicesFound = (ListView) findViewById(R.id.devicesfound); 
    btArrayAdapter = new ArrayAdapter<String>(AndroidBluetooth.this, 
      android.R.layout.simple_list_item_1); 
    listDevicesFound.setAdapter(btArrayAdapter); 

    CheckBlueToothState(); 

    btnScanDevice.setOnClickListener(btnScanDeviceOnClickListener); 

    registerReceiver(ActionFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 

    listDevicesFound.setOnItemClickListener(new OnItemClickListener() 
    { 
     public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
     { 
      Log.i("Discovery: ", Integer.toString(myBtAdapter.getState())); 
      boolean success = myBtAdapter.cancelDiscovery(); 
      myBtDevice = btDevicesFound.get(arg2); 
      try { 
       btSocket = myBtDevice.createRfcommSocketToServiceRecord(MY_UUID); 
       iStream = btSocket.getInputStream(); 
       oStream = btSocket.getOutputStream(); 
      } catch (IOException e) { 
       Log.e("Bluetooth Socket", "Bluetooth not available, or insufficient permissions"); 
      } catch (NullPointerException e) { 
       Log.e("Bluetooth Socket", "Null Pointer One"); 
      } 
      CheckBlueToothState(); 
      try { 
       btSocket.connect(); 
      } catch (IOException e) { 
       Log.e("Bluetooth Socket", "Problems arose while attempting to connect."); 
      } catch (NullPointerException e) { 
       Log.e("Bluetooth Socket", "Null Pointer Two"); 
      } 
     } 

    }); 
} 

private void CheckBlueToothState() { 
    if(myBtAdapter == null) { 
     stateBluetooth.setText("Bluetooth NOT supported"); 
    } else { 
     if(myBtAdapter.isEnabled()) { 
      if(myBtAdapter.isDiscovering()) { 
       stateBluetooth.setText("Bluetooth is currently " + 
         "in device discovery process."); 
      } else { 
       stateBluetooth.setText("Bluetooth is Enabled."); 
       btnScanDevice.setEnabled(true); 
      } 
     } else { 
      stateBluetooth.setText("Bluetooth is NOT enabled"); 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     } 
    } 
} 

private Button.OnClickListener btnScanDeviceOnClickListener = new Button.OnClickListener() { 
    public void onClick(View arg0) { 
     btArrayAdapter.clear(); 
     myBtAdapter.startDiscovery(); 
    } 
}; 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode == REQUEST_ENABLE_BT) { 
     CheckBlueToothState(); 
    } 
} 

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if(BluetoothDevice.ACTION_FOUND.equals(action)) { 
      BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      btDevicesFound.add(btDevice); 
      btArrayAdapter.add(btDevice.getName() + "\n" + btDevice.getAddress()); 
      btArrayAdapter.notifyDataSetChanged(); 
     }   
    } 
}; 
public static void startBluetooth(){ 
    try { 
     myBtAdapter = BluetoothAdapter.getDefaultAdapter(); 
     myBtAdapter.enable(); 
    } catch (NullPointerException ex) { 
     Log.e("Bluetooth", "Device not available"); 
    } 
} 

public static void stopBluetooth() { 
    myBtAdapter.disable(); 
} 
} 

답변

0

내 프로젝트를 정리했는데이 스레드에서 응답을 찾는 모든 사용자에게 잘 돌아갔다.

관련 문제