package com.clj.blesample.tool.operation; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.clj.blesample.R; import com.clj.blesample.tool.BluetoothService; import java.util.ArrayList; import java.util.List; public class CharacteristicListFragment extends Fragment { private ResultAdapter mResultAdapter; private BluetoothService mBluetoothService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBluetoothService = ((OperationActivity) getActivity()).getBluetoothService(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_characteric_list, null); initView(v); return v; } private void initView(View v) { mResultAdapter = new ResultAdapter(getActivity()); ListView listView_device = (ListView) v.findViewById(R.id.list_service); listView_device.setAdapter(mResultAdapter); listView_device.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { final BluetoothGattCharacteristic characteristic = mResultAdapter.getItem(position); final List<Integer> propList = new ArrayList<>(); List<String> propNameList = new ArrayList<>(); int charaProp = characteristic.getProperties(); if ((charaProp & BluetoothGattCharacteristic.PROPERTY_READ) > 0) { propList.add(CharacteristicOperationFragment.PROPERTY_READ); propNameList.add("Read"); } if ((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) { propList.add(CharacteristicOperationFragment.PROPERTY_WRITE); propNameList.add("Write"); } if ((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0) { propList.add(CharacteristicOperationFragment.PROPERTY_WRITE_NO_RESPONSE); propNameList.add("Write No Response"); } if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) { propList.add(CharacteristicOperationFragment.PROPERTY_NOTIFY); propNameList.add("Notify"); } if ((charaProp & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) { propList.add(CharacteristicOperationFragment.PROPERTY_INDICATE); propNameList.add("Indicate"); } if (propList.size() > 1) { new AlertDialog.Builder(getActivity()) .setTitle("选择操作类型") .setItems(propNameList.toArray(new String[propNameList.size()]), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mBluetoothService.setCharacteristic(characteristic); mBluetoothService.setCharaProp(propList.get(which)); ((OperationActivity) getActivity()).changePage(2); } }) .show(); } else if (propList.size() > 0) { mBluetoothService.setCharaProp(propList.get(0)); mBluetoothService.setCharacteristic(characteristic); ((OperationActivity) getActivity()).changePage(2); } } }); } public void showData() { BluetoothGattService service = mBluetoothService.getService(); mResultAdapter.clear(); for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) { mResultAdapter.addResult(characteristic); } mResultAdapter.notifyDataSetChanged(); } private class ResultAdapter extends BaseAdapter { private Context context; private List<BluetoothGattCharacteristic> characteristicList; ResultAdapter(Context context) { this.context = context; characteristicList = new ArrayList<>(); } public void addResult(BluetoothGattCharacteristic characteristic) { characteristicList.add(characteristic); } public void clear() { characteristicList.clear(); } @Override public int getCount() { return characteristicList.size(); } @Override public BluetoothGattCharacteristic getItem(int position) { if (position > characteristicList.size()) return null; return characteristicList.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView != null) { holder = (ViewHolder) convertView.getTag(); } else { convertView = View.inflate(context, R.layout.adapter_service, null); holder = new ViewHolder(); convertView.setTag(holder); holder.txt_title = (TextView) convertView.findViewById(R.id.txt_title); holder.txt_uuid = (TextView) convertView.findViewById(R.id.txt_uuid); holder.txt_type = (TextView) convertView.findViewById(R.id.txt_type); holder.img_next = (ImageView) convertView.findViewById(R.id.img_next); } BluetoothGattCharacteristic characteristic = characteristicList.get(position); String uuid = characteristic.getUuid().toString(); holder.txt_title.setText(String.valueOf("特征" + "(" + position + ")")); holder.txt_uuid.setText(uuid); StringBuilder property = new StringBuilder(); int charaProp = characteristic.getProperties(); if ((charaProp & BluetoothGattCharacteristic.PROPERTY_READ) > 0) { property.append("Read"); property.append(" , "); } if ((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) { property.append("Write"); property.append(" , "); } if ((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0) { property.append("Write No Response"); property.append(" , "); } if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) { property.append("Notify"); property.append(" , "); } if ((charaProp & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) { property.append("Indicate"); property.append(" , "); } if (property.length() > 1) { property.delete(property.length() - 2, property.length() - 1); } if (property.length() > 0) { holder.txt_type.setText(String.valueOf("特性" + "( " + property.toString() + ")")); holder.img_next.setVisibility(View.VISIBLE); } else { holder.img_next.setVisibility(View.INVISIBLE); } return convertView; } class ViewHolder { TextView txt_title; TextView txt_uuid; TextView txt_type; ImageView img_next; } } }