2016-04-07 25 views
0

Android programlamasında yeniyim ve şimdi bir sorunum var. Android telefonumu bir bluetooth sunucusu olarak kullanmak istiyorum. Bu, özel bir Etkinlik açtığımda telefonun diğer Bluetooth cihazlarını (zaten eşleştirilmiş) dinlemesi ve diğer cihazın da android telefonuma bir bağlantı açabilmesi anlamına geliyor. Bu yüzden telefonun bt hoparlör gibi bir flipheral cihaz gibi olması gerekir. Yani eşleştirme hala yapıldı. Ve SPP modunu kullanmam gerektiğini öğrendim. Bağlandığında basit bir bayt akışı almak istiyorum. 'Bluetooth spp araçları pro' (https://play.google.com/store/apps/details?id=mobi.dzs.android.BLE_SPP_PRO&hl=de) adlı bir uygulamayı buldum ve neredeyse istediğim her şeyi yapıyor. Ama burada sorun, telefonların istemci olarak çalışması ve bağlantıyı açmasıdır.Android Bluetooth SPP Sunucusu

Belki bana yardım edebilir ve ne yapmam gerektiğini anlamak için bana bazı ipuçları verebilirsin. yardım :)

DÜZENLEME için

Teşekkür: Şimdi Android'den bir örnek çalıştı ve neredeyse çalışıyor gibi görünüyor. PC'mi Smartphone'la bağlayabilirim ve akıllı telefon da buna bağlanır.

public class BluetoothServer extends AppCompatActivity { 

public BluetoothAdapter mBluetoothAdapter; 
private static final UUID MY_UUID_SECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

private AcceptThread mSecureAcceptThread; 

private TextView textViewStatus; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_bluetooth_server); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    textViewStatus = (TextView) findViewById(R.id.BluetoothServerTextView); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    mSecureAcceptThread.cancel(); 
    mSecureAcceptThread = null; 
} 

public void onClickOpenBluetoothServer(View view) { 

    // Setup Bluetooth Adapter 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

    //Enable Discoverbility 
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); 
    startActivity(discoverableIntent); 
    textViewStatus.append("\nDiscoverable Added!"); 

    //Starting Accepting Thread 
    mSecureAcceptThread = new AcceptThread(); 
    mSecureAcceptThread.start(); 
} 


/** 
* Accepting Thread mit run(); 
*/ 
private class AcceptThread extends Thread { 
    private final BluetoothServerSocket mmServerSocket; 

    public AcceptThread() { 
     BluetoothServerSocket tmp = null; 
     try { 
      // MY_UUID is the app's UUID string, also used by the client code 
      tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("PAKSAFE", MY_UUID_SECURE); 
     } catch (IOException e) { } 
     mmServerSocket = tmp; 
     textViewStatus.append("\nAcceptThread Created!"); 
    } 

    public void run() { 
     BluetoothSocket socket = null; 
     // Keep listening until exception occurs or a socket is returned 
     while (true) { 
      try { 
       textViewStatus.append("\nListening"); 
       socket = mmServerSocket.accept(); 
      } catch (IOException e) { 
       break; 
      } 
      // If a connection was accepted 
      if (socket != null) { 
       // Do work to manage the connection (in a separate thread) 
       //manageConnectedSocket(socket); 
       textViewStatus.setText("Acccepted"); 
       try { 
        mmServerSocket.close(); 
       } catch (IOException e) { 
        break; 
       } 
       break; 
      } 
     } 
    } 

    /** Will cancel the listening socket, and cause the thread to finish */ 
    public void cancel() { 
     try { 
      mmServerSocket.close(); 
     } catch (IOException e) { } 
    } 
} 

}

+0

bulunabilir. İkinci soruları yeni soruya taşıyın. Ne denediğini göster, daha fazla bilgi ver. SO'da bu kadar geniş bir soruyu sormak kurallara aykırıdır. – deimus

cevap

0

bir RFCOMM oluşturun: o, uygulama çökmeleri bağlandı ve bilmiyorum ne zaman ma uygulama kilitleniyor .. Burada kod neden Ama neden ... belki bana söyleyebilir soket (Seri Bağlantı Noktası Profili (SPP) olarak da bilinir) ve gelen verileri dinler.

Android, bu amaçla bir API sahiptir, BluetoothServerSocket kullanın ve gelen verileri dinleyin.

BluetoothServerSocket kullanımının bir örneği orijinal soru güncellenmiş soruyla ilgisi yoktur here

İlgili konular