2016-07-30 140 views
0

做了一個藍牙掃描應用程序,我想添加那些被掃描的藍牙在數據庫中,就像我需要一個按鈕,當用戶單擊該按鈕時,所有在列表視圖中掃描藍牙到數據庫中,我是新來的Java和Android也 任何幫助,將不勝感激如何從列表中獲取數據並將其放入數據庫android

package com.example.test.test; 

    import android.app.Activity; 
    import android.bluetooth.BluetoothAdapter; 
    import android.bluetooth.BluetoothDevice; 
    import android.content.BroadcastReceiver; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.content.IntentFilter; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.AdapterView; 
    import android.widget.ArrayAdapter; 
    import android.widget.Button; 
    import android.widget.ListView; 
    import android.widget.Spinner; 
    import android.widget.Toast; 

    import java.util.Set; 

    public class MainActivity extends Activity { 


      Spinner spinner; 
      ArrayAdapter<CharSequence> adapter; 
      ArrayAdapter<CharSequence>adapter1; 
     ArrayAdapter<CharSequence>adapter2; 

      ListView listDevicesFound; 
      Button btnScanDevice; 
      BluetoothAdapter bluetoothAdapter; 
      int REQUEST_CODE = 1; 

     ArrayAdapter<String>btArrayAdapter; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 




     spinner=(Spinner) findViewById(R.id.spinner); 
       adapter=ArrayAdapter.createFromResource(this,R.array.DEPARTMENT,android.R.layout.simple_spinner_item); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(adapter); 
     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+"selected",Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     }); 
     spinner=(Spinner) findViewById(R.id.spinner2); 
     adapter1=ArrayAdapter.createFromResource(this,R.array.batch,android.R.layout.simple_spinner_item); 
     adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(adapter1); 
     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+"selected",Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     }); 
     spinner=(Spinner) findViewById(R.id.spinner3); 
     adapter2=ArrayAdapter.createFromResource(this,R.array.group,android.R.layout.simple_spinner_item); 
     adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(adapter2); 
     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+"selected",Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     }); 
     btnScanDevice = (Button)findViewById(R.id.scandevice); 
     bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

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

     btnScanDevice.setOnClickListener(btnScanDeviceOnClickListener); 

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


    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy();unregisterReceiver(ActionFoundReceiver); 
    } 
    private void CheckBlueToothState() 
    { 
     if (bluetoothAdapter == null) 
     { 
      Toast.makeText(getBaseContext(),"Bluetooth Not Supported",Toast.LENGTH_LONG).show(); 

     } 
     else 
     { 
      if (bluetoothAdapter.isEnabled()) 
      { 
       if(bluetoothAdapter.isDiscovering()) 
       { 
        Toast.makeText(getBaseContext(),"Bluetooth is currently in device discover process",Toast.LENGTH_LONG).show(); 
       } 
       else 
       { 
        Toast.makeText(getBaseContext(),"Bluetooth is Enabled",Toast.LENGTH_LONG).show(); 
        btnScanDevice.setEnabled(true); 
       } 
      } 
      else 
      { 

        Toast.makeText(getBaseContext(), "Bluetooth is not Enabled", Toast.LENGTH_LONG).show(); 
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
        startActivityForResult(enableBtIntent, REQUEST_CODE); 

      } 
     } 
    } 
    private Button.OnClickListener btnScanDeviceOnClickListener= new Button.OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 

      btArrayAdapter.clear(); 
      if(bluetoothAdapter.startDiscovery()) 
      { 
       Toast.makeText(getBaseContext(),"Scanning", Toast.LENGTH_LONG).show(); 
      } 

     }}; 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(requestCode == REQUEST_CODE) 
     { 
      CheckBlueToothState(); 
     } 
    } 
    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); 
       btArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
       btArrayAdapter.notifyDataSetChanged(); 
      } 
     }}; 
} 

回答

0

你可以做到這樣: 添加數據baseHelper

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.provider.BaseColumns; 


public class DatabaseHelper extends SQLiteOpenHelper { 

private final String TABLE_NAME = "scans" 

private final String KEY_DATA = "data" 

public DatabaseHelper(Context context) { 
    super(context, "BluetoothData", null, 1); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" (" 
      + BaseColumns._ID 
      + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DATA + " VARCHAR)"); 
} 

public void addScanRecord(String record){ 
    SQLiteDatabase database = this.getWritableDatabase(); 
    db.execSQL("INSERT INTO " + TABLE_NAME + " (" + KEY_DATA+ ") VALUES ('" + record + "')"); 
    database.close(); 
} 

} 

添加getter給你適配器:

public List<String> getAllData(){ 
     return myAdapterList; 
} 

的OnClick:

List<String> bluetoothRecords = btArrayAdapter.getAllData(); 

DatabaseHelper bdHelper= new DatabaseHelper (this); 
for(String record : bluetoothRecords){ 
    bdHelper.addScanRecord(record); 
} 
+0

我需要一個按鈕過於時用戶點擊該按鈕添加scaned數據會去數據庫? –

+0

public class DatabaseHelper extends SQLiteOpenHelper ...此行拋出一個錯誤類必須聲明爲抽象或實現抽象方法錯誤 –

+0

「我需要一個按鈕,當用戶單擊該按鈕時,所有在列表視圖中掃描藍牙,而去數據庫」我以爲你想添加額外的按鈕。 但您不必添加額外的按鈕,您可以隨時將數據添加到數據庫。 – patrick

相關問題